Skip to content

Commit 273275b

Browse files
fix: file limit bug
1 parent 9548fd7 commit 273275b

3 files changed

Lines changed: 87 additions & 36 deletions

File tree

ui/src/components/dynamics-form/items/upload/LocalFileUpload.vue

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class="w-full"
77
drag
88
multiple
9-
v-bind:file-list="modelValue"
9+
v-bind:file-list="fileArray"
1010
action="#"
1111
:auto-upload="false"
1212
:show-file-list="false"
@@ -62,7 +62,7 @@
6262
</span>
6363
</div>
6464
<el-row :gutter="8" v-if="fileArray?.length" class="mt-8">
65-
<template v-for="(item, index) in fileArray" :key="index">
65+
<template v-for="(item, index) in sortedFileArray" :key="index">
6666
<el-col :span="12" class="mb-8">
6767
<el-card
6868
shadow="never"
@@ -95,7 +95,7 @@
9595
<el-button v-if="item.canRetry" text @click="uploadFile(item)">
9696
<AppIcon iconName="app-refresh"></AppIcon>
9797
</el-button>
98-
<el-button text @click="deleteFile(index, item)">
98+
<el-button text @click="deleteFile(item)">
9999
<AppIcon iconName="app-delete"></AppIcon>
100100
</el-button>
101101
</div>
@@ -151,6 +151,21 @@ const uploadingCount = computed(
151151
const retryList = computed(() =>
152152
fileArray.value.filter((i: any) => i.status === 'error' && i.canRetry),
153153
)
154+
const getFileStatusOrder = (item: any) => {
155+
if (item.status === 'error' && item.canRetry) return 0
156+
if (item.status === 'error') return 1
157+
if (item.status === 'uploading') return 2
158+
return 3
159+
}
160+
const sortedFileArray = computed(() =>
161+
fileArray.value
162+
.map((item: any, index: number) => ({ item, index }))
163+
.sort(
164+
(a: any, b: any) =>
165+
getFileStatusOrder(a.item) - getFileStatusOrder(b.item) || a.index - b.index,
166+
)
167+
.map(({ item }: any) => item),
168+
)
154169
// 重新上传所有可重试的失败文件
155170
const retryAll = () => {
156171
retryList.value.forEach((i: any) => uploadFile(i))
@@ -165,7 +180,13 @@ const fileHandleChange = (file: any, fileList: UploadFiles) => {
165180
fileList.splice(index, 1)
166181
}
167182
}
183+
if (fileArray.value.length >= file_count_limit.value) {
184+
onExceed()
185+
removeCurrentFile()
186+
return false
187+
}
168188
const item = reactive({
189+
uid: file.uid,
169190
name: file.name,
170191
size: file.size,
171192
file_id: '',
@@ -186,6 +207,7 @@ const fileHandleChange = (file: any, fileList: UploadFiles) => {
186207
// MsgError(t('views.document.tip.fileLimitSizeTip1') + file_size_limit.value + 'MB')
187208
// fileList.splice(-1, 1) //移除当前超出大小的文件
188209
fileArray.value?.push(item)
210+
removeCurrentFile()
189211
return false
190212
}
191213
if (!file_type_list.value.includes(fileType(file.name).toLocaleUpperCase())) {
@@ -203,6 +225,7 @@ const fileHandleChange = (file: any, fileList: UploadFiles) => {
203225
}
204226
205227
fileArray.value?.push(item)
228+
removeCurrentFile()
206229
uploadFile(item)
207230
}
208231
// 执行上传
@@ -238,15 +261,18 @@ const uploadFile = (item: any) => {
238261
item.canRetry = true
239262
})
240263
}
241-
function deleteFile(index: any, item?: any) {
264+
function deleteFile(item: any) {
242265
// 上传过程中删除则中断上传请求
243266
if (item?.status === 'uploading' && typeof item.abort === 'function') {
244267
item.aborted = true
245268
item.abort()
246269
} else if (item?.status === 'success' && item?.file_id) {
247270
applicationApi.deleteFile(item.file_id)
248271
}
249-
fileArray.value.splice(index, 1)
272+
const index = fileArray.value.indexOf(item)
273+
if (index !== -1) {
274+
fileArray.value.splice(index, 1)
275+
}
250276
emit('update:modelValue', fileArray.value)
251277
}
252278

ui/src/views/document/upload/UploadComponent.vue

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@
209209
</span>
210210
</div>
211211
<el-row :gutter="8" v-if="form.fileList?.length" class="mt-8">
212-
<template v-for="(item, index) in form.fileList" :key="index">
212+
<template v-for="(item, index) in sortedFileList" :key="index">
213213
<el-col :span="12" class="mb-8">
214214
<el-card
215215
shadow="never"
@@ -242,7 +242,7 @@
242242
<el-button v-if="item.canRetry" text @click="uploadFile(item)">
243243
<AppIcon iconName="app-refresh"></AppIcon>
244244
</el-button>
245-
<el-button text @click="deleteFile(index, item)">
245+
<el-button text @click="deleteFile(item)">
246246
<AppIcon iconName="app-delete"></AppIcon>
247247
</el-button>
248248
</div>
@@ -317,13 +317,30 @@ const uploadingCount = computed(
317317
const retryList = computed(() =>
318318
form.value.fileList.filter((i: any) => i.status === 'error' && i.canRetry),
319319
)
320+
const getFileStatusOrder = (item: any) => {
321+
if (item.status === 'error' && item.canRetry) return 0
322+
if (item.status === 'error') return 1
323+
if (item.status === 'uploading') return 2
324+
return 3
325+
}
326+
const sortedFileList = computed(() =>
327+
form.value.fileList
328+
.map((item: any, index: number) => ({ item, index }))
329+
.sort(
330+
(a: any, b: any) =>
331+
getFileStatusOrder(a.item) - getFileStatusOrder(b.item) || a.index - b.index,
332+
)
333+
.map(({ item }: any) => item),
334+
)
320335
const retryAll = () => {
321336
retryList.value.forEach((i: any) => uploadFile(i))
322337
}
323-
338+
const filterSuccessFiles = (data: any): any => {
339+
return data?.filter((f: any) => f.status === 'success') || []
340+
}
324341
watch(form.value, (value) => {
325342
knowledge.saveDocumentsType(value.fileType)
326-
knowledge.saveDocumentsFile(value.fileList)
343+
knowledge.saveDocumentsFile(filterSuccessFiles(value.fileList))
327344
})
328345
329346
function downloadTemplate(type: string) {
@@ -350,14 +367,17 @@ function radioChange() {
350367
form.value.fileList = []
351368
}
352369
353-
function deleteFile(index: number | string, item?: any) {
370+
function deleteFile(item: any) {
354371
if (item?.status === 'uploading' && typeof item.abort === 'function') {
355372
item.aborted = true
356373
item.abort()
357374
} else if (item?.status === 'success' && item?.file_id) {
358375
applicationApi.deleteFile(item.file_id)
359376
}
360-
form.value.fileList.splice(index, 1)
377+
const index = form.value.fileList.indexOf(item)
378+
if (index !== -1) {
379+
form.value.fileList.splice(index, 1)
380+
}
361381
}
362382
363383
// 上传on-change事件
@@ -370,14 +390,36 @@ const fileHandleChange = (file: any, fileList: UploadFiles) => {
370390
fileList.splice(index, 1)
371391
}
372392
}
393+
if (form.value.fileList.length >= file_count_limit.value) {
394+
onExceed()
395+
removeCurrentFile()
396+
return false
397+
}
398+
const item = reactive({
399+
uid: file.uid,
400+
name: file.name,
401+
size: file.size,
402+
file_id: '',
403+
source_file_id: '',
404+
percentage: 0,
405+
status: 'uploading' as 'uploading' | 'success' | 'error',
406+
errMsg: '',
407+
canRetry: false,
408+
raw: file.raw,
409+
abort: null as null | (() => void),
410+
aborted: false,
411+
})
373412
//1、判断文件大小是否合法,文件限制不能大于100M
374413
const isLimit = file?.size / 1024 / 1024 < file_size_limit.value
375414
if (!isLimit) {
376-
MsgError(t('views.document.tip.fileLimitSizeTip1') + file_size_limit.value + 'MB')
377-
removeCurrentFile() //移除当前超出大小的文件
415+
item.status = 'error'
416+
item.errMsg = t('dynamicsForm.UploadInput.errorTip.sizeError')
417+
// MsgError(t('views.document.tip.fileLimitSizeTip1') + file_size_limit.value + 'MB')
418+
// fileList.splice(-1, 1) //移除当前超出大小的文件
419+
form.value.fileList?.push(item)
420+
removeCurrentFile()
378421
return false
379422
}
380-
381423
if (!isRightType(file?.name, form.value.fileType)) {
382424
if (file?.name !== '.DS_Store') {
383425
MsgError(t('views.document.upload.errorMessage2'))
@@ -391,20 +433,8 @@ const fileHandleChange = (file: any, fileList: UploadFiles) => {
391433
return false
392434
}
393435
394-
const item = reactive({
395-
name: file.name,
396-
size: file.size,
397-
file_id: '',
398-
source_file_id: '',
399-
percentage: 0,
400-
status: 'uploading' as 'uploading' | 'success' | 'error',
401-
errMsg: '',
402-
canRetry: false,
403-
raw: file.raw,
404-
abort: null as null | (() => void),
405-
aborted: false,
406-
})
407436
form.value.fileList.push(item)
437+
removeCurrentFile()
408438
uploadFile(item)
409439
}
410440
@@ -464,14 +494,6 @@ const handlePreview = (bool: boolean) => {
464494
*/
465495
function validate() {
466496
if (!FormRef.value) return
467-
if (uploadingCount.value) {
468-
MsgError(t('dynamicsForm.UploadInput.uploading'))
469-
return false
470-
}
471-
if (errorCount.value) {
472-
MsgError(t('dynamicsForm.UploadInput.failedStatus', { count: errorCount.value }))
473-
return false
474-
}
475497
return FormRef.value.validate((valid: any) => {
476498
return valid
477499
})

ui/src/views/knowledge-workflow/component/action/DataSource.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,11 @@ const base_form_data_rule = ref<FormRules>({
143143
const validate = () => {
144144
return dynamicsFormRef.value?.validate()
145145
}
146+
const filterSuccessFiles = (data: any): any => {
147+
return data?.file_list?.filter((f: any) => f.status === 'success') || []
148+
}
146149
const get_data = () => {
147-
return form_data.value
150+
return filterSuccessFiles(form_data.value)
148151
}
149152
watch(
150153
source_node_list,

0 commit comments

Comments
 (0)