【发布时间】:2021-02-11 16:17:45
【问题描述】:
尝试通过一个带有多文件上传的 XMLHttpRequest 获取多个进度条状态。无法使用 jQuery,并且由于客户端限制,我没有选项循环文件并在循环中包含 new XMLHttpRequests。
目前我可以为所有文件组合一个进度条。我需要的是多个进度条百分比。如果我在 fileObj 循环中执行 request.upload.onprogress,那么只会更新最后一个进度条。
let formData = new FormData();
let request = new XMLHttpRequest();
request.open('POST', postUrl);
for( var x in fileObj){
formData.append("file", fileObj[x]);
//this is where I tried to put the request.upload.onprogress with dynamic ids but failed.
}
request.upload.onprogress = function (e) {
if (e.lengthComputable) {
var percentComplete = (e.loaded / e.total) * 100;
d.getElementById("progress-bar" ).style.width = percentComplete + "%";
d.getElementById('progress-bar-output').innerHTML = Math.round(percentComplete) + "%";
}
};
request.onload = function (e) {
console.log( JSON.parse( e.target.response ) );
};
request.send(formData);
【问题讨论】:
-
单个 XHR 有一个进度 - 您可以计算每个文件的上传长度,这样您就可以计算单个进度
-
是
fileObjFileList对象吗?