Skip to content

File upload improvements #155

@kamsar

Description

@kamsar

The default file upload configuration expects custom file upload markup, e.g.:

 <button class="custom-file-upload" type="button">Select file</button>
 <input type="file" name="Media"/>

This is a bit confusing to have this unwritten markup convention (and it also display:nones bare input type=file). It also gives zero indication to the user that they've selected a file.

I propose two modifications:

  1. Add an additional tag to store the currently selected file name, if any. I've implemented this on a project thusly:

HTML:

<button class="custom-file-upload" type="button">Select file</button>
<span class="filename js-hidden"></span>
<input type="file" name="Media"/>

JS:

$('.custom-file-upload').click(function(e) {
e.preventDefault();

$(this).nextAll('input[type="file"]')
        .click()
        .change(function() {
            var val = $(this).val();
            if(val.length > 0) {
                $(this).prev('.filename').html(val).show();
            }
        });
  });

And secondly, to add some JS to automatically wrap input type=file with the expected markup, so that you do not need to know the details to successfully markup a file upload.

Lastly, does this code really belong in a file called documentReady.js? I mean, I know it's most performant to have one ready function, but it's terrible separation of concerns.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions