控制并发请求

dev
truthhun 1 year ago
parent 2e55fee878
commit af8f3f288a

@ -123,6 +123,7 @@
<template slot="header">
操作 (<el-button
type="text"
size="mini"
:disabled="loading"
@click="clearAllFiles"
>清空</el-button
@ -426,49 +427,22 @@ export default {
window.uploadDocumentCancel = []
} catch (error) {}
//
this.fileList.map(async (file) => {
if (file.percentage === 100 && file.attachment_id) {
console.log('跳过已上传成功的文件', file.name)
return
}
file.error = ''
file.progressStatus = 'success'
const formData = new FormData()
formData.append('file', file.raw)
try {
const res = await uploadDocument(formData, {
onUploadProgress: (progressEvent) => {
file.percentage = parseInt(
(progressEvent.loaded / progressEvent.total) * 100
)
},
// timeout: 1000 * 6,
// chrome 6 fileList 2便 2
const fileList = this.fileList.reduce((prev, cur, index) => {
const i = Math.floor(index / 2)
prev[i] = prev[i] || []
prev[i].push(cur)
return prev
}, [])
console.log(fileList)
fileList.reduce(async (prev, cur) => {
await prev
await Promise.all(
cur.map(async (file) => {
await this.uploadDocument(file)
})
if (res.status === 200) {
file.attachment_id = res.data.data.id || 0
this.createDocument(file)
this.totalSuccess++
} else {
file.progressStatus = 'exception'
file.error = res.data.message || res.statusText
this.$message.error(`${file.name}${file.error}`)
this.totalFailed++
}
} catch (error) {
file.progressStatus = 'exception'
file.error = '上传失败或超时,请重试'
this.$message.error(`${file.name}${file.error}`)
this.totalFailed++
}
this.totalDone++
if (this.totalDone === this.totalFiles) {
this.loading = false
}
})
)
}, Promise.resolve())
}
})
},
@ -480,6 +454,48 @@ export default {
this.filesMap = {}
this.$refs.upload.clearFiles()
},
async uploadDocument(file) {
if (file.percentage === 100 && file.attachment_id) {
console.log('跳过已上传成功的文件', file.name)
return
}
file.error = ''
file.progressStatus = 'success'
const formData = new FormData()
formData.append('file', file.raw)
try {
const res = await uploadDocument(formData, {
onUploadProgress: (progressEvent) => {
file.percentage = parseInt(
(progressEvent.loaded / progressEvent.total) * 100
)
},
// timeout: 1000 * 6,
})
if (res.status === 200) {
file.attachment_id = res.data.data.id || 0
this.createDocument(file)
this.totalSuccess++
} else {
file.progressStatus = 'exception'
file.error = res.data.message || res.statusText
this.$message.error(`${file.name}${file.error}`)
this.totalFailed++
}
} catch (error) {
file.progressStatus = 'exception'
file.error = '上传失败或超时,请重试'
this.$message.error(`${file.name}${file.error}`)
this.totalFailed++
}
this.totalDone++
if (this.totalDone === this.totalFiles) {
this.loading = false
}
},
async createDocument(doc) {
const createDocumentRequest = {
overwrite: this.document.overwrite,

Loading…
Cancel
Save