mirror of
https://github.com/vbenjs/vue-vben-admin.git
synced 2025-01-24 02:00:25 +08:00
feat(components->Upload): 修正图片上传组件允许自定义上传格式限制 (#3755)
This commit is contained in:
parent
36274025d6
commit
302e2125ba
@ -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;
|
||||
});
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user