Skip to content

Commit 98e73fe

Browse files
committed
fix: 🐛 修复上传文件多选文件时,上传失败的问题
修复上传文件多选文件时,上传失败的问题
1 parent 6204deb commit 98e73fe

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/components/Upload/FileUpload.vue

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
UploadRawFile,
4747
UploadUserFile,
4848
UploadFile,
49+
UploadFiles,
4950
UploadProgressEvent,
5051
UploadRequestOptions,
5152
} from "element-plus";
@@ -147,7 +148,7 @@ watch(
147148
function handleBeforeUpload(file: UploadRawFile) {
148149
// 限制文件大小
149150
if (file.size > props.maxFileSize * 1024 * 1024) {
150-
ElMessage.warning("上传图片不能大于" + props.maxFileSize + "M");
151+
ElMessage.warning("上传文件不能大于" + props.maxFileSize + "M");
151152
return false;
152153
}
153154
return true;
@@ -190,10 +191,34 @@ const handleProgress = (event: UploadProgressEvent) => {
190191
/**
191192
* 上传成功
192193
*/
193-
const handleSuccess = (fileInfo: FileInfo) => {
194+
const handleSuccess = (response: any, uploadFile: UploadFile, files: UploadFiles) => {
194195
ElMessage.success("上传成功");
195-
196-
modelValue.value = [...modelValue.value, { name: fileInfo.name, url: fileInfo.url } as FileInfo];
196+
//只有当状态为success或者fail,代表文件上传全部完成了,失败也算完成
197+
if (
198+
files.every((file: UploadFile) => {
199+
return file.status === "success" || file.status === "fail";
200+
})
201+
) {
202+
let fileInfos = [] as FileInfo[];
203+
files.map((file: UploadFile) => {
204+
if (file.status === "success") {
205+
//只取携带response的才是刚上传的
206+
let res = file.response as FileInfo;
207+
if (res) {
208+
fileInfos.push({ name: res.name, url: res.url } as FileInfo);
209+
}
210+
} else {
211+
//失败上传 从fileList删掉,不展示
212+
fileList.value.splice(
213+
fileList.value.findIndex((e) => e.uid === file.uid),
214+
1
215+
);
216+
}
217+
});
218+
if (fileInfos.length > 0) {
219+
modelValue.value = [...modelValue.value, ...fileInfos];
220+
}
221+
}
197222
};
198223
199224
/**

src/components/Upload/SingleImageUpload.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
:http-request="handleUpload"
1111
:on-success="onSuccess"
1212
:on-error="onError"
13-
multiple
1413
>
1514
<template #default>
1615
<el-image v-if="modelValue" :src="modelValue" />

0 commit comments

Comments
 (0)