使用 Summernote File 一次上传多个文件到服务器
Upload multiple files at once to server with Summernote File
我可以对下面的代码进行哪些更改,以便我的 upload.php
文件可以接收多个文件,我会循环处理这些文件?我一直在尝试使用 Summernote 一次将多个文件上传到服务器。虽然,我 select 从我的笔记本电脑上传了多个文件,但只上传了一个文件。我确信处理上传的 PHP 文件不是问题,因为即使我 select 多个文件要上传,它也只接收一个文件。下面是 JQuery 代码的样子
$('.summernote-mini').summernote({
height: 200,
tabsize: 2,
callbacks: {
onFileUpload: function(files) {
callBack(files[0]);
},
}
});
回调函数
function callBack(files) {
let data = new FormData();
data.append('media_upload[]', files);
$.ajax({
data: data,
type: "POST",
url: "upload.php",
cache: false,
contentType: false,
processData: false,
xhr: function() { //Handle progress upload
let myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) myXhr.upload.addEventListener('progress', progressHandlingFunction, false);
return myXhr;
}
}).done(function(reponse){
//I handle the response here
});
}
无论我 select 上传多少文件,count($_FILES['media_upload']['name'])
都会在我的 upload.php
文件中提供 1
来处理服务器端文件上传。这个问题的解决方案是什么?
在@Fravadona 的帮助下,我找到了问题的解决方案。
我按照@Fravadona的建议用callBack(files);
替换了callBack(files[0]);
。
然后,在回调函数中,我用下面的代码替换了 data.append('media_upload[]', files);
:
var iLength = files.length;
var i;
for(i = 0; i < iLength; i++){
data.append("media_upload[]", files[i]);
}
所以正确的代码变成了这样:
$('.summernote-mini').summernote({
height: 200,
tabsize: 2,
callbacks: {
onFileUpload: function(files) {
callBack(files);
},
}
});
而回调函数变成了这样:
function callBack(files) {
let data = new FormData();
var iLength = files.length;
var i;
for(i = 0; i < iLength; i++){
data.append("media_upload[]", files[i]);
}
$.ajax({
data: data,
type: "POST",
url: "upload.php",
cache: false,
contentType: false,
processData: false,
xhr: function() { //Handle progress upload
let myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) myXhr.upload.addEventListener('progress', progressHandlingFunction, false);
return myXhr;
}
}).done(function(reponse){
//I handle the response here
});
}
现在,我可以使用 Summmernote 一次成功上传多个文件了。
我可以对下面的代码进行哪些更改,以便我的 upload.php
文件可以接收多个文件,我会循环处理这些文件?我一直在尝试使用 Summernote 一次将多个文件上传到服务器。虽然,我 select 从我的笔记本电脑上传了多个文件,但只上传了一个文件。我确信处理上传的 PHP 文件不是问题,因为即使我 select 多个文件要上传,它也只接收一个文件。下面是 JQuery 代码的样子
$('.summernote-mini').summernote({
height: 200,
tabsize: 2,
callbacks: {
onFileUpload: function(files) {
callBack(files[0]);
},
}
});
回调函数
function callBack(files) {
let data = new FormData();
data.append('media_upload[]', files);
$.ajax({
data: data,
type: "POST",
url: "upload.php",
cache: false,
contentType: false,
processData: false,
xhr: function() { //Handle progress upload
let myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) myXhr.upload.addEventListener('progress', progressHandlingFunction, false);
return myXhr;
}
}).done(function(reponse){
//I handle the response here
});
}
无论我 select 上传多少文件,count($_FILES['media_upload']['name'])
都会在我的 upload.php
文件中提供 1
来处理服务器端文件上传。这个问题的解决方案是什么?
在@Fravadona 的帮助下,我找到了问题的解决方案。
我按照@Fravadona的建议用callBack(files);
替换了callBack(files[0]);
。
然后,在回调函数中,我用下面的代码替换了 data.append('media_upload[]', files);
:
var iLength = files.length;
var i;
for(i = 0; i < iLength; i++){
data.append("media_upload[]", files[i]);
}
所以正确的代码变成了这样:
$('.summernote-mini').summernote({
height: 200,
tabsize: 2,
callbacks: {
onFileUpload: function(files) {
callBack(files);
},
}
});
而回调函数变成了这样:
function callBack(files) {
let data = new FormData();
var iLength = files.length;
var i;
for(i = 0; i < iLength; i++){
data.append("media_upload[]", files[i]);
}
$.ajax({
data: data,
type: "POST",
url: "upload.php",
cache: false,
contentType: false,
processData: false,
xhr: function() { //Handle progress upload
let myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) myXhr.upload.addEventListener('progress', progressHandlingFunction, false);
return myXhr;
}
}).done(function(reponse){
//I handle the response here
});
}
现在,我可以使用 Summmernote 一次成功上传多个文件了。