From bc56384325b15f5d649bdde20d5d117f2080d98e Mon Sep 17 00:00:00 2001 From: JEECG <445654970@qq.com> Date: Thu, 20 Mar 2025 10:08:20 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90issues/7954=E3=80=91BasicUpload?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=EF=BC=8C?= =?UTF-8?q?=E9=99=90=E5=88=B6=E4=B8=8A=E4=BC=A0=E6=A0=BC=E5=BC=8F=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E5=87=BA=E9=94=99=20---?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/Upload/src/helper.ts | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/jeecgboot-vue3/src/components/Upload/src/helper.ts b/jeecgboot-vue3/src/components/Upload/src/helper.ts index a0c574b7..f3664948 100644 --- a/jeecgboot-vue3/src/components/Upload/src/helper.ts +++ b/jeecgboot-vue3/src/components/Upload/src/helper.ts @@ -1,9 +1,43 @@ export function checkFileType(file: File, accepts: string[]) { - const newTypes = accepts.join('|'); - // const reg = /\.(jpg|jpeg|png|gif|txt|doc|docx|xls|xlsx|xml)$/i; - const reg = new RegExp('\\.(' + newTypes + ')$', 'i'); - - return reg.test(file.name); + // update-begin--author:liaozhiyang---date:20250318---for:【issues/7954】BasicUpload组件上传文件,限制上传格式校验出错 + const mimePatterns: string[] = []; + const suffixList: string[] = []; + // 分类处理 accepts + for (const item of accepts) { + if (item.includes('/')) { + mimePatterns.push(item); + } else { + // 支持.png 或 png(带点后缀或者不带点后缀) + const suffix = item.startsWith('.') ? item.slice(1) : item; + suffixList.push(suffix); + } + } + // 后缀匹配逻辑 + let suffixMatch = false; + if (suffixList.length > 0) { + const suffixRegex = new RegExp(`\\.(${suffixList.join('|')})$`, 'i'); + suffixMatch = suffixRegex.test(file.name); + } + // MIME类型匹配逻辑 + let mimeMatch = false; + if (mimePatterns.length > 0 && file.type) { + mimeMatch = mimePatterns.some((pattern) => { + // 先转义特殊字符,再处理通配符 + const regexPattern = pattern + .replace(/[.+?^${}()|[\]\\]/g, '\\$&') // 先转义特殊字符 + .replace(/\*/g, '.*'); // 再替换通配符 + const regex = new RegExp(`^${regexPattern}$`, 'i'); + return regex.test(file.type); + }); + } + if (mimePatterns.length && suffixList.length) { + return suffixMatch || mimeMatch; + } else if (mimePatterns.length) { + return mimeMatch; + } else if (suffixList.length) { + return suffixMatch; + } + // update-end--author:liaozhiyang---date:20250318---for:【issues/7954】BasicUpload组件上传文件,限制上传格式校验出错 } export function checkImgType(file: File) {