Skip to content

Commit 3f7b9a7

Browse files
committed
fix(file-upload): 修复file-upload组件多选合并上传的bug
1 parent d5cec34 commit 3f7b9a7

File tree

1 file changed

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

1 file changed

+29
-10
lines changed

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

Lines changed: 29 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) => {
245254
if (state.isEdm && file.name.length > 255) {
246255
remove({ api, file, autoRemove })
247256
return Modal.message({
@@ -254,16 +263,26 @@ 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+
file.raw = fileRow
279+
} else {
280+
fileType = getFileType({ file })
281+
282+
if (accept) {
283+
const isExist = isAcceptType(acceptArray, file, constants, fileType)
284+
!isExist && (isValid = false)
285+
}
267286
}
268287

269288
if (state.triggerClickType && types) {
@@ -612,7 +631,7 @@ export const handleStart =
612631
const handler = (file) =>
613632
vm.$refs[constants.UPLOAD_INNER].$refs[constants.UPLOAD_INNER_TEMPLATE].upload(file.raw)
614633

615-
rawFiles.length && api.beforeUpload({ raw: rawFiles }, true, handler)
634+
rawFiles.length && api.beforeUpload({ raw: rawFiles }, true, handler, true)
616635
} else {
617636
rawFiles.forEach((rawFile) => {
618637
const file = api.getFile(rawFile)

0 commit comments

Comments
 (0)