function makeCall(file, handlerFile, sendMethod, formData) {
//console.log(instance.files);
$.ajax({
url: handlerFile,
type: sendMethod,
xhr: function() { // Custom XMLHttpRequest
var xhr = $.ajaxSettings.xhr();
if(xhr.upload) { // Check if upload property exists
xhr.upload.addEventListener('progress', progressHandlingFunction.bind(file)); // For handling the progress of the upload
//xhr.upload.addEventListener('loadend', successHandler.bind(xhr));
}
//console.log(xhr);
return xhr;
},
beforeSend: beforeSendHandler.bind(file),
success: completeHandler.bind(file),
//error: errorHandler,
data: formData, // Form data
dataType: 'json',
cache: true,
//async: false,
contentType: false,
processData: false
});
}
$(".afu-input").on('change', function() {
var i = 0;
var length = this.files.length;
var files = this.files;
var calling = setInterval(function() {
var file = files[i];
console.log(file);
uid = generateUniqueId();
file.id = uid;
var formData = null;
formData = new FormData();
formData.append(i, file);
formData.append(i, [uid]);
makeCall(file, handlerFile, sendMethod, formData);
i++;
if (i >= length) {
clearInterval(calling);
}
}, 2000); // Delay is for testing. Normaly there is no delay
}
我正在尝试一个一个上传文件。但问题是 ajax 正确发送请求,但只返回一个(第一个)数据。
我的目标是创建在 input[type=file] 更改时上传图像(以及后来的其他文件)的脚本。我尝试通过一个请求上传多个文件,但进度事件将它们注册为一个。我有想法将文件切成块并发送,但这个变体似乎更容易(只需要让它工作,然后我改进它)。谢谢。