html标记:
<input id="fileSelect" type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
我正在用php上传多个文件。我想制作一个上传文件数组,并使用ajax发送到服务器。如何使多个所选文件组成数组?
JavaScript:
jQuery.ajax({
url: 'insertfiles.php',
type: "POST",
data: {
file: // array of selected files.
},
success: function(data){
},
error: function(data){
alert( 'Sorry.' );
}
});
支持HTML5文件内容的现代浏览器在<input>
元素中具有“文件”属性。这将为您提供文件列表参考,该参考具有一个length
属性。
由于该属性已经是,array
因此您只需要访问它或对其进行迭代。
JS
var input = document.getElementById('id');
console.log(input.files);
for (var i = 0; i < input.files.length; i++) {
console.log(input.files[i]);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句