Click here to Skip to main content
15,910,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ,

i have this html code

HTML
<input type="file" name="myfiles[]" class="required"/>


i need to alert file name which user choose so i use this

JavaScript
$("input[type='file']").change(function(){
      alert($(this).val());	
});


and it works fine

what if i have a file input with multiple attr like below

HTML
<input type="file" multiple  name="myfiles[]" class="required"/>


How can i alert all files names ?
Posted

1 solution

For browsers which support multiple file selection[^], you'll need to use the files property[^]:
JavaScript
$("input[type='file']").change(function(){
    if (this.files){
        alert($.map(this.files, function(f){ return f.name; }).join("\n"));
    }
    else {
        alert(this.value);
    }
});

In supported browsers, each item in the files array is a File[^] object, which has other useful properties you might want to look at.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900