Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "HTML <input type='file' />" has "files" property in JavaScript?

Consider this HTML snippet:

<input type='file' id='fileUpload' />

To get access to this control in JavaScript, we can write:

var temp = document.getElementById('fileUpload');

OK, don't get mad, I know you all know this. But the interesting part is that, temp variable now has a property called files, (not file, but files, the plural form) which is of type FileList, which of course is a list of File objects. This semantically should mean that uploading multiple files via one and only one HTML file upload control should be possible. However, you can't upload multiple files this way and there are many workarounds, not straight usage.

How do you explain this paradox?

Update: I built this jsfiddle to see one of the answers in action.

like image 659
Saeed Neamati Avatar asked Sep 13 '11 03:09

Saeed Neamati


People also ask

How do you specify a file type in HTML input?

<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.

Can we upload file using JavaScript?

html file through a browser, the client will be able to upload a file to the server using Ajax and pure JavaScript. A pure JavaScript file uploader simplifies Ajax based interactions with the server.

What is the value of input type file?

The input element, having the "file" value in its type attribute, represents a control to select a list of one or more files to be uploaded to the server. When the form is submitted, the selected files are uploaded to the server, along with their name and type.


2 Answers

In HTML5, The multiple attribute specifies that multiple values can be selected for an input field.

<input type='file' multiple='multiple' />

These links should help:

http://www.w3schools.com/html5/html5_form_attributes.asp

http://rakaz.nl/2009/08/uploading-multiple-files-using-html5.html

like image 123
Chris Pietschmann Avatar answered Nov 13 '22 10:11

Chris Pietschmann


According to the Gecko DOM Reference, it is only applicable when using the file upload element via drag-n-drop.

like image 36
Wyatt Avatar answered Nov 13 '22 10:11

Wyatt