vue循环发起请求,等一个请求结束后,进行下一次请求
async await new Promise
async filesSubmitted(files, fileList) {
if (files.length === 0) {
return this.$message.error("文件列表存在同名文件,请关闭文件列表后再试。");
}
for (let file of files) {
const path = this.filePath
const name = file.relativePath
const size = file.size
var formData = new FormData();
formData.append('concurrentType', this.fileType)
formData.append('fileName', path + name)
formData.append('partCount', Math.ceil(size / this.chunkSize))
formData.append('fileSize', size)
formData.append('userId',this.createUser)
formData.append('type',1)
if (this.fileSetCode) {
formData.append('datasetCode', this.fileSetCode)
}
let res = await this.submitFile(formData, file)
}
this.isAllCover = -1
},
// 文件提交
submitFile(formData, file) {
return new Promise((resolve, reject) => {
this.axios.post(api['distributed'].uploadTestFile, formData
).then(response => {
let res = response.data
if (res.code === 200) {
setTimeout(() => {
file.chunkUrlData = res.data.ulist
file.chunkUrlData.uploadId = res.data.uploadId
file.path = res.data.fileName
var code = res.data.paperCode + '/'
if (this.menuItem.length == 0) {
this.filePath = '论文/' + code
} else {
this.filePath = this.menuItem[this.menuItem.length - 1].path
}
this.dataSetCode = code
this.$emit('getCurrentDataSetCode', code)
// 存在重复文件
if (res.data.isExist) {
if (this.isAllCover === 1) { // 已选择全部替换
this.startUpload(file, false)
resolve()
} else if (this.isAllCover === -1) { // 询问是否需要全部替换
this.$confirm('存在重复文件, 是否全部替换?', '提示', {
confirmButtonText: '全部替换',
cancelButtonText: '逐一替换',
type: 'warning'
}).then(() => {
this.isAllCover = 1
this.startUpload(file, false)
resolve()
}).catch(() => {
this.isAllCover = 0 // 选择逐一替换
this.$confirm(file.name + '已存在, 是否选择替换?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.startUpload(file, false)
resolve()
}).catch(() => {
this.startUpload(file, true)
resolve()
});
});
} else if (this.isAllCover === 0) {
this.$confirm(file.name + '已存在, 是否选择替换?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.startUpload(file, false)
resolve()
}).catch(() => {
this.startUpload(file, true)
resolve()
});
}
// this.startUpload(file, res.data.isExist)
} else {
this.startUpload(file, res.data.isExist)
this.$emit('deletePath', res.data.fileName)
resolve()
}
}, 1000);
} else {
}
})
.catch(function (error) {
console.log(error);
});
})
},