From e07e925c4228480f1678c55b752525fc664af9ed Mon Sep 17 00:00:00 2001 From: linxd <544554903@qq.com> Date: Tue, 5 Aug 2025 15:02:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E7=BB=84=E4=BB=B6=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E6=96=87=E4=BB=B6=E6=A0=BC=E5=BC=8F=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/FileUpload/FileUpload.tsx | 4 ++-- src/utils/utils.ts | 23 ++--------------------- 2 files changed, 4 insertions(+), 23 deletions(-) diff --git a/src/components/FileUpload/FileUpload.tsx b/src/components/FileUpload/FileUpload.tsx index 0e6a371..fc2b973 100644 --- a/src/components/FileUpload/FileUpload.tsx +++ b/src/components/FileUpload/FileUpload.tsx @@ -50,7 +50,7 @@ const FileUpload: React.FC = ({ listType = 'text', buttonText, disabled = false, - accept, + accept = allowedTypes.map(type => `.${type}`).join(','), showUploadList = true, isDragger = false, tip, @@ -163,7 +163,7 @@ const FileUpload: React.FC = ({ }; const beforeUpload = (file: File) => { - return validateFileSize(file, maxSize, allowedTypes); + return validateFileSize(file, maxSize); }; const UploadComponent = isDragger ? Upload.Dragger : Upload; diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 8e7223b..ddcdaa4 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -4,33 +4,14 @@ import { message,Upload } from 'antd'; // 参数说明: // file: 上传的文件 // maxSize: 最大文件大小 -// type: 文件类型,支持*表示所有类型 -const FileTypeMap = { - 'application/pdf': 'pdf', - 'image/jpeg': 'jpg', - 'image/png': 'png', - 'application/msword': 'doc', - 'application/zip': 'zip', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'docx', - 'application/vnd.ms-excel': 'xls', - 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'xlsx', - 'application/vnd.ms-powerpoint': 'ppt', - 'application/vnd.openxmlformats-officedocument.presentationml.presentation': 'pptx', - -} -export const validateFileSize = (file: File, maxSize: number,type: string[]) => { +export const validateFileSize = (file: File, maxSize: number) => { const { LIST_IGNORE } = Upload; const isLtMaxSize = file.size / 1024 / 1024 < maxSize; if (!isLtMaxSize) { message.error(`文件大小不能超过${maxSize}MB!`); return LIST_IGNORE; } - const isValidFormat = type.includes('*') || type.includes(FileTypeMap[file.type as keyof typeof FileTypeMap]); - if (!isValidFormat) { - message.error(`只能上传${type.join(',')}格式文件!`); - return LIST_IGNORE; - } - return isValidFormat && isLtMaxSize; + return isLtMaxSize; };