These days we can drag & drop files into a special container and upload them with XHR 2. Many at a time. With live progress bars etc. Very cool stuff. Example here.
But sometimes we don't want that much coolness. What I'd like is to drag & drop files -- many at a time -- into a standard HTML file input: <input type=file multiple>
.
Is that possible? Is there some way to 'fill' the file input with the right filenames (?) from the file drop? (Full filepaths aren't available for file system security reasons.)
Why? Because I'd like to submit a normal form. For all browsers and all devices. The drag & drop is just progressive enhancement to enhance & simplify UX. The standard form with standard file input (+ multiple
attribute) will be there. I'd like to add the HTML5 enhancement.
edit
I know in some browsers you can sometimes (almost always) drop files into the file input itself. I know Chrome usually does this, but sometimes it fails and then loads the file in the current page (a big fail if you're filling out a form). I want to fool- & browserproof it.
I made a solution for this.
The Drag and Drop functionality for this method only works with Chrome, Firefox and Safari. (Don't know if it works with IE10), but for other browsers, the "Or click here" button works fine.
The input field simply follow your mouse when dragging a file over an area, and I've added a button as well..
Uncomment opacity:0; the file input is only visible so you can see what's going on.
Awesome work by @BjarkeCK. I made some modifications to his work, to use it as method in jquery:
Working Fiddle
I know some trick works in Chrome.
When you dropping a files into drop zone you got a dataTransfer.files object, that is a "FileList" type of object, that contains all the files you dragged. Meanwhile, element has property "files", that is the same "FileList" type object.
So, you can simply assing the dataTransfer.files object to input.files property.
In theory, you could add an element overlaying the
<input/>
, and then use it'sdrop
event to capture the files (using the File API) and pass them to inputfiles
array.Except that a file input is read-only. This is an old problem.
You can however, bypass the form control completely and upload via XHR (not sure about the support for that):
You could also use an element in the surrounding area to cancel the drop event in Chrome, and prevent the default behaviour of loading the file.
Dropping multiple files over the input already works in Safari and Firefox.
For anyone who's looking to do this in 2018, I've got a much better and simpler solution then all the old stuff posted here. You can make a nice looking drag & drop box with just vanilla HTML, JavaScript and CSS.
(Only works in Chrome so far)
Let's start with the HTML.
Then we'll get to the styling.
After you've done this it already looks fine. But I imagine you'd like to see what file you actaully uploaded, so we're going to do some JavaScript. Remember that pfp-value span? That's where we'll print out the file name.
And that's it.