希望这个问题有一个简单的解决方案。
我需要一次上传一个文件到我的服务器,所以我有一个脚本,通过为每个文件输入字段通过iframe排队文件上传。
现在,我有一个工作脚本循环并单独上传每个文件,选择:
<input type="file" name="file" />
<input type="file" name="file" />
<input type="file" name="file" />
像这样循环:
$('input[type="file"]').each(function(){
我的问题是,如何更改 .each 以使用此功能,只需一个文件选择器:
<input type="file" name="file" mulitple/>
$(???).each(function(){
我试过了
fileInput = document.getElementById("multiFileChooser");
$(fileInput.files).each(function(){
但是文件没有发送,也没有出现在请求的有效负载中。
编辑2:
固定和工作解决方案:
(function($){$(document).ready(function(){
$('input[type="submit"]').click(function(){ //on button press
for(var ii = 0; ii < $('#multiChoose').get(0).files.length; ii++){
sendFile($('#multiChoose').get(0).files[ii]);
//wait until upload complete to start next one
}
});
})})(jQuery);
function sendFile(file) {
var uri = "upload.php";
var xhr = new XMLHttpRequest();
var fd = new FormData();
xhr.open("POST", uri, true);
xhr.onreadystatechange = function() {
//queue next file
};
fd.append('file', file);
xhr.send(fd);
}