diff --git a/src/components/Upload/src/components/ImageUpload.vue b/src/components/Upload/src/components/ImageUpload.vue index cc1d83de3..1b3a74baf 100644 --- a/src/components/Upload/src/components/ImageUpload.vue +++ b/src/components/Upload/src/components/ImageUpload.vue @@ -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'; + 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; @@ -170,9 +170,9 @@ name: props.name, filename: props.filename, }); - if(props.resultField){ + if (props.resultField) { info.onSuccess!(res); - }else{ + } else { // 不传入 resultField 的情况 info.onSuccess!(res.data); } @@ -190,8 +190,8 @@ const list = (fileList.value || []) .filter((item) => item?.status === UploadResultStatus.DONE) .map((item: any) => { - if(props.resultField){ - return get(item?.response, props.resultField) + if (props.resultField) { + return get(item?.response, props.resultField); } return item?.url || item?.response?.url; }); diff --git a/src/components/Upload/src/helper.ts b/src/components/Upload/src/helper.ts index 7e8494e90..ea7cc88dc 100644 --- a/src/components/Upload/src/helper.ts +++ b/src/components/Upload/src/helper.ts @@ -1,8 +1,11 @@ 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'); - + let reg; + if (!accepts || accepts.length === 0) { + reg = /.(jpg|jpeg|png|gif|webp)$/i; + } else { + const newTypes = accepts.join('|'); + reg = new RegExp('\\.(' + newTypes + ')$', 'i'); + } return reg.test(file.name); }