如何使用jquery获取html 5多文件上传变量的值?

时间:2022-12-19 20:33:59

even though i select multiple files using below html.

即使我在html下面选择了多个文件。

<input type="file" id="multiplefiles" name="uploadedfile[]" multiple>

I only get value of first file selected. i am using a simple:

我只获得选定的第一个文件的值。我用的很简单:

var filelist = $("#multiplefiles").val() || [];
$.each(filelist, function(i, myfile) {
  console.log('found file '+i+' ='+myfile);
});

please advise how do i get list of all files...

请告知我如何获得所有文件的列表……

for example selected string in the input field is: C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg, C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg, C:\Users\Public\Pictures\Sample Pictures\upload-2.txt

例如选择字符串的输入字段是:C:\Users\Public\Pictures\Sample图片\ Hydrangeas.jpg C:\Users\Public\Pictures\Sample图片\ Chrysanthemum.jpg C:\Users\Public\Pictures\Sample \图片上传- 2. - txt

and from above logic i only get: following in log:

从上面的逻辑我只得到:

found file 0 =Hydrangeas.jpg

ty. Rajeev

泰拉杰夫。

1 个解决方案

#1


8  

This should do the trick:

这应该可以做到:

var filelist = document.getElementById("multiplefiles").files || [];
for (var i = 0; i < filelist.length; i++) {
    console.log('found file ' + i + ' = ' + filelist[i].name);
}

A working jsFiddle is here.

一个工作的jsFiddle在这里。

#1


8  

This should do the trick:

这应该可以做到:

var filelist = document.getElementById("multiplefiles").files || [];
for (var i = 0; i < filelist.length; i++) {
    console.log('found file ' + i + ' = ' + filelist[i].name);
}

A working jsFiddle is here.

一个工作的jsFiddle在这里。