I am using an input file element to upload file in my JSP page. The user can upload the file by clicking on a button which opens a form with browser's browse button
Now, I want to upload the file by using drag drop feature of HTML5 (as described @ html5 rocks page). I am able to extract the file name using File API. Is there a way to load the input file element with the dropped file using File API? (So that I may use the same behavior to upload file)
In HTML, we will use the type attribute to take input in a form and when we have to take the file as an input, the file value of the type attribute allows us to define an element for the file uploads.
<input type="file"> <input> elements with type="file" let the user choose one or more files from their device storage. Once chosen, the files can be uploaded to a server using form submission, or manipulated using JavaScript code and the File API.
There's no way to set the value of a <input type=file>
directly in JS. That action
requires user intervention (and a click on a file picker). However, you can use
xhr.send(FormData)
or xhr.send(File)
to send the files. See my response here:
HTML5 File API readAsBinaryString reads files as much larger, different than files on disk
In your drop handler, just pas the FileList
obtained from evt.dataTransfer.files
to that uploadFiles()
helper.
I was able to set the "files" attribute of the input element with the files property of the dataTransfer object.
document.getElementById("insert input file id").files = evt.dataTransfer.files;
I just tried using getelement.value and it errors. Set the files property with the files list.
Happy Programming!
Kremnari
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With