feat(components->Upload): 修正图片上传组件允许自定义上传格式限制 (#3755)

This commit is contained in:
1455668754 2024-04-18 14:10:32 +08:00 committed by GitHub
parent 36274025d6
commit 302e2125ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 13 deletions

View File

@ -35,14 +35,15 @@
import { useI18n } from '@/hooks/web/useI18n';
import { useUploadType } from '../hooks/useUpload';
import { uploadContainerProps } from '../props';
import { isImgTypeByName } from '../helper';
import { checkFileType } from '../helper';
import { UploadResultStatus } from '@/components/Upload/src/types/typing';
import { get, omit } from 'lodash-es';
defineOptions({ name: 'ImageUpload' });
const emit = defineEmits(['change', 'update:value', 'delete']);
const props = defineProps({
...omit(uploadContainerProps,["previewColumns","beforePreviewData"]),
...omit(uploadContainerProps, ['previewColumns', 'beforePreviewData']),
});
const { t } = useI18n();
const { createMessage } = useMessage();
@ -138,8 +139,7 @@
const beforeUpload = (file: File) => {
const { maxSize, accept } = props;
const { name } = file;
const isAct = isImgTypeByName(name);
const isAct = checkFileType(file, accept);
if (!isAct) {
createMessage.error(t('component.upload.acceptUpload', [accept]));
isActMsg.value = false;
@ -191,7 +191,7 @@
.filter((item) => item?.status === UploadResultStatus.DONE)
.map((item: any) => {
if (props.resultField) {
return get(item?.response, props.resultField)
return get(item?.response, props.resultField);
}
return item?.url || item?.response?.url;
});

View File

@ -1,8 +1,11 @@
export function checkFileType(file: File, accepts: string[]) {
let reg;
if (!accepts || accepts.length === 0) {
reg = /.(jpg|jpeg|png|gif|webp)$/i;
} else {
const newTypes = accepts.join('|');
// const reg = /\.(jpg|jpeg|png|gif|txt|doc|docx|xls|xlsx|xml)$/i;
const reg = new RegExp('\\.(' + newTypes + ')$', 'i');
reg = new RegExp('\\.(' + newTypes + ')$', 'i');
}
return reg.test(file.name);
}