从多个文件上载字段中获取文件名 [英] Get the file names from multiple file upload field
问题描述
我正在尝试使用Jquery编写一个ajax上传器。
在html中我可以写一个多上传字段,如:
I am trying to write an ajax uploader using Jquery. In the html I can write a multiple upload field like:
<input type='file' multiple='multiple' name='files[]'>
但是,如何从此字段中获取值?
当我使用时:
But, how can I get the values from this field? When I used:
$('input[type='file']').val();
或
$('input[type='file']').attr('value');
我只获得了我选择上传的第一个文件的文件名。有没有办法让我得到一个包含所有文件的数组?
I only got the file name for the first file I selected to upload. Is there a way I can get an array containing all files?
推荐答案
HTML5文件API 表示输入
将具有文件
属性。您可以像这样访问它:
The HTML5 Files API means that the input
will have a files
property. You can access it like so:
var fileList = document.getElementById("yourInput").files;
返回 FileList
,我相信它是一个类似于数组的对象,包含用户选择的所有文件,所以你可以像往常一样迭代它:
That returns a FileList
, which I believe is an array-like object containing all files selected by the user, so you would be able to iterate over it as normal:
for(var i = 0; i < fileList.length; i++) {
//Do something with individual files
}
但是在不支持HTML5的浏览器中不起作用,我认为在版本9之前它不支持IE。如果那些旧的浏览器对你很重要,最好的办法是使用Flash或基于Java的上传器。
But it won't work in browsers that don't support HTML5, and I don't think it was supported in IE until version 9. If those older browsers are important to you, your best bet is probably to use a Flash or Java based uploader.
这篇关于从多个文件上载字段中获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!