[英]Upload multiple files with JQuery blueimp
I am trying to perform multiple fileupload with blueimp jquery fileupload library. 我正在尝试使用blueimp jquery fileupload库执行多个文件上传。 link to library - https://github.com/blueimp/jQuery-File-Upload 链接到库-https: //github.com/blueimp/jQuery-File-Upload
The files are uploading. 文件正在上传。 But only a single file is uploading at a time. 但是一次只能上传一个文件。 Multiple files are also uploading but single file is getting uploaded once. 多个文件也正在上传,但单个文件一次已上传。 I am not sure what the error is. 我不确定这是什么错误。
PHP PHP
<?php
// A list of permitted file extensions
$allowed = array('png', 'jpg', 'gif','zip', 'html','php', 'py', 'jpeg', 'txt');
for ($i=0; $i < count($_FILES); $i++) {
if(isset($_FILES['files']) && $_FILES['files'][$i]['error'] == 0){
$extension = pathinfo($_FILES['files'][$i]['name'], PATHINFO_EXTENSION);
if(!in_array(strtolower($extension), $allowed)){
echo '{"status":"error 1", "extension": "'.$extension.'"}';
exit;
}
if(move_uploaded_file($_FILES['files'][$i]['tmp_name'], __DIR__.'/upl/'.$_FILES['files'][$i]['name'])){
echo '{"status": "success"}';
}
}
else {
echo '{"status":"error"}';
exit;
}
}
?>
HTML HTML
<div class="message">
<h1>Files to be uploaded: </h1>
</div>
<div class="result">
</div>
<input id="fileupload" type="file" name="files[]" multiple>
<br><br>
<input type="submit" name="submit" id="upload">
JS JS
$(function () {
$('#fileupload').fileupload({
dataType: 'json',
url: 'act',
add: function(e,data) {
console.log("added for uploading");
console.log(data.files[0].name);
if( $(".message").is(":hidden") ) {
$(".message").show();
}
$(".message").append("<p>"+ data.files[0].name + "</p>");
$("#upload").click(function(){
data.submit();
})
},
start: function (e) {
console.log('start');
},
done: function (e, data) {
console.log("success in uploading data");
console.log(data);
console.log(data.files[0].name);
// $.each(data.files, function (index, file) {
// $('<p/>').text("file name: " + file.name).appendTo($(".message"));
// });
$(".message").hide();
$(".result").append("Uploading files");
},
fail: function (e, data) {
console.log(data);
console.log("failed to perform");
$.each(data.messages, function (index, error) {
$('<p style="color: red;">Upload file error: ' + error + '<i class="elusive-remove" style="padding-left:10px;"/></p>')
.appendTo('.message');
});
}
});
});
Multiple files are getting uploaded. 多个文件正在上传。 but a single file is getting uploaded at a time. 但一次只能上传一个文件。 I mean if I submit 3 files, first file only will be sent first for uploading, then second file will be sent and finally third file will be sent. 我的意思是,如果我提交了3个文件,则只会首先发送第一个文件进行上传,然后发送第二个文件,最后发送第三个文件。 Instead of sending all the three files as an array. 而不是将所有三个文件作为数组发送。 Each time a single file will be sent. 每次将发送一个文件。 ie totally 3 arrays will be sent. 即总共将发送3个数组。 And also can anyone help about performing chunking. 而且任何人都可以帮助您执行分块。 Thanks!! 谢谢!!
UPDATE There is a problem in appending data to data.files, instead of appending files to the array it's creating a new object every time. 更新将数据附加到data.files中是有问题的,而不是每次都将文件创建到数组中而不是将文件附加到数组中。 But I am not sure why it is done! 但是我不确定为什么要这样做!
Finally after reading documentation a bit. 最后看了一点文档。 I found it out that generally blueimp fileuploader makes one request per file. 我发现,通常blueimp fileuploader每个文件发出一个请求。 That is the reason why it is uploading each file different times whenever I upload different files. 这就是为什么每次我上传不同文件时,它都会不同时间上传每个文件的原因。
To process multiple files for same request. 为同一请求处理多个文件。
$('#fileupload').fileupload({
dataType: 'json',
autoUpload: false,
url: 'act',
maxChunkSize: 10000000,
maxFileSize: 1000000000,
singleFileUploads: false,
singleFileUploads: false
is actually disabling single file uploads. singleFileUploads: false
实际上是禁用单个文件上传。 I got it from documentation here -- https://github.com/blueimp/jQuery-File-Upload/wiki/Options#singlefileuploads 我从这里的文档中获得了它-https: //github.com/blueimp/jQuery-File-Upload/wiki/Options#singlefileuploads
Thanks for all the help 谢谢你的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.