67

Possible Duplicate:
Retrieving file names out of a multi-file upload control with javascript

From HTML5 input type="file" allows users to choose multiple files by adding the multiple="multiple" :

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

My question is: how can I get the value of that input? When using the .value it only returns the filename of the first file selected, but when choosing more than one I am not able to view the other ones.

What I have:

<input type="file" multiple="multiple" onchange="alert(this.value)"
     onmouseout="alert(this.value) />

which as I told you, is only showing the name of one of the selected files.

NOTE: I don't want to edit the value (I know that is not possible) only the name of the files

Thanks!

0

2 Answers 2

120

The files selected are stored in an array: [input].files

For example, you can access the items

// assuming there is a file input with the ID `my-input`...
var files = document.getElementById("my-input").files;

for (var i = 0; i < files.length; i++)
{
 alert(files[i].name);
}

For jQuery-comfortable people, it's similarly easy

// assuming there is a file input with the ID `my-input`...
var files = $("#my-input")[0].files;

for (var i = 0; i < files.length; i++)
{
 alert(files[i].name);
}
Sign up to request clarification or add additional context in comments.

4 Comments

the files property of the file input is undefined on ie, though works just fine on other browsers. do you know any way to go around this?
files[i] is undefined in IE9. Can you give solution on how to get this work in IE9 ?
@OO7 IE9 doesn't support multiple input file. It is IE10+ only.
name is without path, what property is for full path?
12

You use input.files property. It's a collection of File objects and each file has a name property:

onmouseout="for (var i = 0; i < this.files.length; i++) alert(this.files[i].name);"

2 Comments

Thanks, it works perfectly the same as freedompeace answer (name or fileName both work) but he answered first and I cannot set two answers as accepted, +1 to your answer anyway
Actually, he answered without a proper code example first and edited his post after I sent mine. But - doesn't really matter. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.