Skip to content

Commit

Permalink
feat(components->Upload): 修正图片上传组件允许自定义上传格式限制 (#3755)
Browse files Browse the repository at this point in the history
  • Loading branch information
1455668754 committed Apr 18, 2024
1 parent 3627402 commit 302e212
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
18 changes: 9 additions & 9 deletions src/components/Upload/src/components/ImageUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
});
Expand Down
11 changes: 7 additions & 4 deletions src/components/Upload/src/helper.ts
Original file line number Diff line number Diff line change
@@ -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);
}

Expand Down

0 comments on commit 302e212

Please sign in to comment.