Skip to content

Commit aadad39

Browse files
author
puhui999
committed
fix: 修复文件上传数据回显错误
1 parent 261d8b2 commit aadad39

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

src/components/UploadFile/src/UploadFile.vue

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@
22
<div class="upload-file">
33
<el-upload
44
ref="uploadRef"
5-
:multiple="props.limit > 1"
6-
name="file"
7-
v-model="valueRef"
85
v-model:file-list="fileList"
9-
:show-file-list="true"
10-
:auto-upload="autoUpload"
116
:action="updateUrl"
7+
:auto-upload="autoUpload"
8+
:before-upload="beforeUpload"
9+
:drag="drag"
1210
:headers="uploadHeaders"
1311
:limit="props.limit"
14-
:drag="drag"
15-
:before-upload="beforeUpload"
16-
:on-exceed="handleExceed"
17-
:on-success="handleFileSuccess"
12+
:multiple="props.limit > 1"
1813
:on-error="excelUploadError"
19-
:on-remove="handleRemove"
14+
:on-exceed="handleExceed"
2015
:on-preview="handlePreview"
16+
:on-remove="handleRemove"
17+
:on-success="handleFileSuccess"
18+
:show-file-list="true"
2119
class="upload-file-uploader"
20+
name="file"
2221
>
23-
<el-button type="primary"><Icon icon="ep:upload-filled" />选取文件</el-button>
22+
<el-button type="primary">
23+
<Icon icon="ep:upload-filled" />
24+
选取文件
25+
</el-button>
2426
<template v-if="isShowTip" #tip>
2527
<div style="font-size: 8px">
2628
大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b>
@@ -35,8 +37,8 @@
3537
<script lang="ts" setup>
3638
import { propTypes } from '@/utils/propTypes'
3739
import { getAccessToken, getTenantId } from '@/utils/auth'
38-
import type { UploadInstance, UploadUserFile, UploadProps, UploadRawFile } from 'element-plus'
39-
import { isArray, isString } from '@/utils/is'
40+
import type { UploadInstance, UploadProps, UploadRawFile, UploadUserFile } from 'element-plus'
41+
import { isString } from '@/utils/is'
4042
4143
defineOptions({ name: 'UploadFile' })
4244
@@ -54,8 +56,8 @@ const props = defineProps({
5456
drag: propTypes.bool.def(false), // 拖拽上传
5557
isShowTip: propTypes.bool.def(true) // 是否显示提示
5658
})
59+
5760
// ========== 上传相关 ==========
58-
const valueRef = ref(props.modelValue)
5961
const uploadRef = ref<UploadInstance>()
6062
const uploadList = ref<UploadUserFile[]>([])
6163
const fileList = ref<UploadUserFile[]>([])
@@ -64,6 +66,7 @@ const uploadHeaders = ref({
6466
Authorization: 'Bearer ' + getAccessToken(),
6567
'tenant-id': getTenantId()
6668
})
69+
6770
// 文件上传之前判断
6871
const beforeUpload: UploadProps['beforeUpload'] = (file: UploadRawFile) => {
6972
if (fileList.value.length >= props.limit) {
@@ -97,12 +100,10 @@ const beforeUpload: UploadProps['beforeUpload'] = (file: UploadRawFile) => {
97100
// 文件上传成功
98101
const handleFileSuccess: UploadProps['onSuccess'] = (res: any): void => {
99102
message.success('上传成功')
100-
const fileListNew = fileList.value
101-
fileListNew.pop()
102-
fileList.value = fileListNew
103+
fileList.value.shift()
103104
uploadList.value.push({ name: res.data, url: res.data })
104105
if (uploadList.value.length == uploadNumber.value) {
105-
fileList.value = fileList.value.concat(uploadList.value)
106+
fileList.value.push(...uploadList.value)
106107
uploadList.value = []
107108
uploadNumber.value = 0
108109
emitUpdateModelValue()
@@ -131,29 +132,25 @@ const handlePreview: UploadProps['onPreview'] = (uploadFile) => {
131132
// 监听模型绑定值变动
132133
watch(
133134
() => props.modelValue,
134-
() => {
135-
const files: string[] = []
135+
(val: string | string[]) => {
136+
if (!val) {
137+
fileList.value = [] // fix:处理掉缓存,表单重置后上传组件的内容并没有重置
138+
return
139+
}
140+
141+
fileList.value = [] // 保障数据为空
136142
// 情况1:字符串
137-
if (isString(props.modelValue)) {
138-
// 情况1.1:逗号分隔的多值
139-
if (props.modelValue.includes(',')) {
140-
files.concat(props.modelValue.split(','))
141-
} else if (props.modelValue.length > 0) {
142-
files.push(props.modelValue)
143-
}
144-
} else if (isArray(props.modelValue)) {
145-
// 情况2:字符串
146-
files.concat(props.modelValue)
147-
} else if (props.modelValue == null) {
148-
// 情况3:undefined 不处理
149-
} else {
150-
throw new Error('不支持的 modelValue 类型')
143+
if (isString(val)) {
144+
fileList.value.push(
145+
...val.split(',').map((url) => ({ name: url.substring(url.lastIndexOf('/') + 1), url }))
146+
)
151147
}
152-
fileList.value = files.map((url: string) => {
153-
return { url, name: url.substring(url.lastIndexOf('/') + 1) } as UploadUserFile
154-
})
148+
// 情况2:数组
149+
fileList.value.push(
150+
...(val as string[]).map((url) => ({ name: url.substring(url.lastIndexOf('/') + 1), url }))
151+
)
155152
},
156-
{ immediate: true }
153+
{ immediate: true, deep: true }
157154
)
158155
// 发送文件链接列表更新
159156
const emitUpdateModelValue = () => {
@@ -166,7 +163,7 @@ const emitUpdateModelValue = () => {
166163
emit('update:modelValue', result)
167164
}
168165
</script>
169-
<style scoped lang="scss">
166+
<style lang="scss" scoped>
170167
.upload-file-uploader {
171168
margin-bottom: 5px;
172169
}

0 commit comments

Comments
 (0)