Skip to content

Commit

Permalink
fix(): upload-files-v2构件限制了上传文件类型时,前端需要二次校验 SHLSTYYWPT-693
Browse files Browse the repository at this point in the history
  • Loading branch information
zhendonghuang authored and zhendonghuang committed Nov 6, 2024
1 parent 6c55c47 commit 4637553
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions bricks/forms/src/i18n/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export enum K {
USE_LINK_TO_UPLOAD_PLACEHOLDER = "USE_LINK_TO_UPLOAD_PLACEHOLDER",
UPLOAD_IMG_NUMBER_LIMIT = "UPLOAD_IMG_NUMBER_LIMIT",
ATTACHMENT = "ATTACHMENT",
NO_SUPPORT_FILE_TYPE = "NO_SUPPORT_FILE_TYPE",
}

export type Locale = { [key in K]: string };
1 change: 1 addition & 0 deletions bricks/forms/src/i18n/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ const locale: Locale = {
[K.UPLOAD_IMG_NUMBER_LIMIT]:
"Only {{maxNumber}} image can be uploaded at most",
[K.ATTACHMENT]: "Attachment",
[K.NO_SUPPORT_FILE_TYPE]: "The current file type is not supported",
};

export default locale;
1 change: 1 addition & 0 deletions bricks/forms/src/i18n/locales/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ const locale: Locale = {
"请输入图片的链接,多个链接之间用换行分隔",
[K.UPLOAD_IMG_NUMBER_LIMIT]: "最多只能上传 {{maxNumber}} 张图片",
[K.ATTACHMENT]: "附件",
[K.NO_SUPPORT_FILE_TYPE]: "当前文件类型不支持",
};

export default locale;
16 changes: 16 additions & 0 deletions bricks/forms/src/upload-files-v2/UploadFilesV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ export function RealUploadFile(
reject(new Error(i18n.t(`${NS_FORMS}:${K.VOLUME_TOO_BIG}`)));
});
}
if (props.accept) {
const acceptedTypes = props.accept.split(",").map((type) => type.trim());
const fileName = file.name.toLowerCase();
const fileType = file.type;
const isValidType = acceptedTypes.some((type) => {
if (type.startsWith(".")) {
return fileName.endsWith(type);
}
return fileType === type;
});
if (!isValidType) {
return new Promise((_resolve, reject) => {
reject(new Error(i18n.t(`${NS_FORMS}:${K.NO_SUPPORT_FILE_TYPE}`)));
});
}
}
if (props.autoUpload) {
// 进行自动上传
return new Promise((resolve) => resolve(file));
Expand Down

0 comments on commit 4637553

Please sign in to comment.