Skip to content

Commit 455e55b

Browse files
authored
fix(file-upload): Fix bug in file-upload component where multiple selections are merge-service and uploaded (#3424)
* fix(file-upload): 修复file-upload组件多选合并上传的bug * fix(file-upload): 增加格式提示
1 parent 5e3aded commit 455e55b

File tree

1 file changed

+32
-10
lines changed
  • packages/renderless/src/file-upload

1 file changed

+32
-10
lines changed

packages/renderless/src/file-upload/index.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ const onBeforeIsPromise = ({
205205
)
206206
}
207207

208+
const isAcceptType = (acceptArray, file, constants, fileType) => {
209+
return acceptArray.some((type) => {
210+
if (type.toLowerCase() === constants.IMAGE_TYPE) {
211+
return constants.FILE_TYPE.PICTURE.split('/').includes(fileType)
212+
}
213+
return new RegExp(`(${type.trim()})$`, 'i').test(file.name)
214+
})
215+
}
216+
208217
const getFileType = ({ file }: { file: IFileUploadFile }): string => {
209218
const { name, url } = file
210219
let fileType = ''
@@ -241,7 +250,7 @@ export const beforeUpload =
241250
t,
242251
state
243252
}: Pick<IFileUploadRenderlessParams, 'props' | 'api' | 'constants' | 't' | 'state'> & IFileUploadModalVm) =>
244-
(file: IFileUploadFile, autoRemove: boolean, doUpload: Function) => {
253+
(file: IFileUploadFile, autoRemove: boolean, doUpload: Function, isMergeUpload = false) => {
245254
if (state.isEdm && file.name.length > 255) {
246255
remove({ api, file, autoRemove })
247256
return Modal.message({
@@ -254,16 +263,29 @@ export const beforeUpload =
254263
let isValid = true
255264
const accept = state.isEdm ? state.accept : props.accept
256265
const types = constants.FILE_TYPE[state.triggerClickType.toUpperCase()]
257-
const fileType = getFileType({ file })
258-
259-
if (accept) {
260-
const isExist = accept.split(',').some((type) => {
261-
if (type.toLowerCase() === constants.IMAGE_TYPE) {
262-
return constants.FILE_TYPE.PICTURE.split('/').includes(fileType)
266+
const acceptArray = accept ? accept.split(',') : []
267+
let fileType = ''
268+
if (isMergeUpload) {
269+
const fileRow = []
270+
fileType = file.raw.flatMap((f) => {
271+
const type = getFileType({ file: f })
272+
if (accept) {
273+
const isExist = isAcceptType(acceptArray, f, constants, type)
274+
isExist ? fileRow.push(f) : remove({ api, file: f, autoRemove })
263275
}
264-
return new RegExp(`(${type.trim()})$`, 'i').test(file.name)
276+
return type
265277
})
266-
!isExist && (isValid = false)
278+
if (!fileRow.length) {
279+
isValid = false
280+
}
281+
file.raw = fileRow
282+
} else {
283+
fileType = getFileType({ file })
284+
285+
if (accept) {
286+
const isExist = isAcceptType(acceptArray, file, constants, fileType)
287+
!isExist && (isValid = false)
288+
}
267289
}
268290

269291
if (state.triggerClickType && types) {
@@ -612,7 +634,7 @@ export const handleStart =
612634
const handler = (file) =>
613635
vm.$refs[constants.UPLOAD_INNER].$refs[constants.UPLOAD_INNER_TEMPLATE].upload(file.raw)
614636

615-
rawFiles.length && api.beforeUpload({ raw: rawFiles }, true, handler)
637+
rawFiles.length && api.beforeUpload({ raw: rawFiles }, true, handler, true)
616638
} else {
617639
rawFiles.forEach((rawFile) => {
618640
const file = api.getFile(rawFile)

0 commit comments

Comments
 (0)