【issues/7954】BasicUpload组件上传文件,限制上传格式校验出错 ---

dev
JEECG 3 months ago
parent d7dc1b8dc7
commit bc56384325

@ -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---forissues/7954BasicUpload
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---forissues/7954BasicUpload
}
export function checkImgType(file: File) {

Loading…
Cancel
Save