现在项目设计到大文件的切片上传,需要在第一片上传的响应状态时200后,才能进行第二片的上传,依次上传,现在通过循环控制需要上传切片的次数,但是现在点击上传以后,所有的请求全部发出去,
应该怎么样才能实现第一片的上传成功以后再开始下一片的上传呢?
现在代码如下:
代码如下:
let iUpdadeState = 0;
this.testFilesStatus.forEach((item, index) => {
console.log("item", item);
let obj = item[0].listFileSliceInfo[0];
if (obj.strStatus == "none") {
iUpdadeState = 0;
} else {
iUpdadeState = 1;
}
let lSize = this.testFiles[index].file.size;
const sectionSize = 1 * 1024 * 1024;
const sectionTotal = Math.ceil(lSize / sectionSize);
for (let i = 0; i < sectionTotal; i++) {
const startSize = i * sectionSize; // 切片开始位置
const endSize =
i === sectionTotal - 1 ? lSize : (i + 1) * sectionSize;
let fileData = this.testFiles[index].file.slice(startSize, endSize);
if (this.progress) {
let formData = new FormData();
formData.append("file", fileData);
formData.append("iUpdadeState", iUpdadeState);
formData.append("iSlideDocumentId", item[0].iFileId);
formData.append("strFilePath", "");
FileUploadHandler(formData, (spreed) => {
// 总大小
let loaded = 0;
loaded += spreed.loaded;
let sep =
(((loaded / this.testFiles[index].file.total) * 100) | 0) + "%";
this.testFiles[index].file.status = sep;
});
}
}
});
这个方法可以改为递归,每次在axios的回调里执行,执行到次数等于sectionTotal就可以了