Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all values of multiple file select with jQuery [duplicate]

Tags:

jquery

file

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

Got this:

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

Using this in jQuery:

$("input[name=file1]").change(function() {
    $("input[name=file]").val($("input[name=file1]").val());
});

Now my problem is this: It only gives the first selected file, I would like to have them all... How to do this? Thanks!

like image 324
Bob van Luijt Avatar asked Nov 30 '12 20:11

Bob van Luijt


3 Answers

Although I linked to a possible dupe in the comments, it's a pure JavaScript solution. Here's a jQuery version if you like: jsFiddle example

$("input[name=file1]").change(function() {
    var names = [];
    for (var i = 0; i < $(this).get(0).files.length; ++i) {
        names.push($(this).get(0).files[i].name);
    }
    $("input[name=file]").val(names);
});​
like image 94
j08691 Avatar answered Nov 04 '22 21:11

j08691


You are looking for the 'files' property, which returns a filelist:

var ele = document.getElementById($(this).attr('id'));
var result = ele.files;

Here is the Fiddle for it

like image 28
DeeDub Avatar answered Nov 04 '22 22:11

DeeDub


Use .length() to get the number of files then use a while statement to do the same for all files, increasing the count by 1 each time :)

like image 23
ABC Avatar answered Nov 04 '22 23:11

ABC