Commit 2bf5ea25 authored by hzl's avatar hzl

feat: 处理并发上传

parent dfd32a5e
...@@ -107,6 +107,11 @@ export default { ...@@ -107,6 +107,11 @@ export default {
headers: { headers: {
type: Object, type: Object,
default: () => ({}) default: () => ({})
},
// 最大并发上传数量
maxConcurrentUploads: {
type: Number,
default: 5
} }
}, },
...@@ -116,6 +121,8 @@ export default { ...@@ -116,6 +121,8 @@ export default {
uploadQueue: [], uploadQueue: [],
successFiles: [], // 存储上传成功的文件信息 successFiles: [], // 存储上传成功的文件信息
isProcessing: false, // 是否有上传中的文件 isProcessing: false, // 是否有上传中的文件
maxConcurrent: this.maxConcurrentUploads, // 最大并发上传数量
activeUploads: 0, // 当前活跃的上传数量
} }
}, },
...@@ -126,6 +133,7 @@ export default { ...@@ -126,6 +133,7 @@ export default {
this.fileList = [] this.fileList = []
this.uploadQueue = [] this.uploadQueue = []
this.successFiles = [] this.successFiles = []
this.activeUploads = 0
}, },
// 处理文件变化 // 处理文件变化
...@@ -223,16 +231,25 @@ export default { ...@@ -223,16 +231,25 @@ export default {
}, },
async processUploadQueue() { async processUploadQueue() {
while (this.uploadQueue.length > 0) { // 并发处理上传队列
if (this.isProcessing) return while (this.uploadQueue.length > 0 && this.activeUploads < this.maxConcurrent) {
this.isProcessing = true const file = this.uploadQueue.shift()
await this.processSingleFile(this.uploadQueue[0]) this.activeUploads++
this.uploadQueue.shift() this.processSingleFile(file).finally(() => {
this.isProcessing = false this.activeUploads--
// 继续处理队列中的其他文件
if (this.uploadQueue.length > 0 && this.activeUploads < this.maxConcurrent) {
this.processUploadQueue()
}
// 检查是否所有文件都已完成
this.checkUploadComplete()
})
} }
},
// 所有文件上传完成后触发事件 // 检查上传是否完成
if (this.uploadQueue.length === 0) { checkUploadComplete() {
if (this.uploadQueue.length === 0 && this.activeUploads === 0) {
this.$emit('upload-complete', this.successFiles) this.$emit('upload-complete', this.successFiles)
} }
}, },
...@@ -339,6 +356,8 @@ export default { ...@@ -339,6 +356,8 @@ export default {
file.cancelToken.cancel('批量取消上传') file.cancelToken.cancel('批量取消上传')
} }
}) })
this.activeUploads = 0
this.uploadQueue = []
}, },
percentFormat(percent){ percentFormat(percent){
return percent == 0 ? '等待中': `${percent}%` return percent == 0 ? '等待中': `${percent}%`
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
:upload-url="uploadUrl" :upload-url="uploadUrl"
:extra-data="extraData" :extra-data="extraData"
:headers="headers" :headers="headers"
:max-concurrent-uploads="10"
@update:files="handleFilesUpdate" @update:files="handleFilesUpdate"
@upload-success="handleUploadSuccess" @upload-success="handleUploadSuccess"
@upload-error="handleUploadError" @upload-error="handleUploadError"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment