File tree Expand file tree Collapse file tree 2 files changed +45
-4
lines changed Expand file tree Collapse file tree 2 files changed +45
-4
lines changed Original file line number Diff line number Diff line change 77 :on-error =" uploadError"
88 :on-success =" uploadSuccess"
99 :on-remove =" uploadRemove"
10+ :before-upload =" checkFile"
1011 :show-file-list =" true"
1112 :limit =" limit"
1213 :accept =" accept"
2829 name: ' UploadCommon'
2930 })
3031
31- defineProps ({
32+ const props = defineProps ({
3233 limit: {
3334 type: Number ,
3435 default: 3
3536 },
3637 accept: {
3738 type: String ,
3839 default: ' '
40+ },
41+ fileSize: {
42+ type: Number ,
43+ default: 8
3944 }
4045 })
4146
6974 }
7075
7176 const uploadRemove = (file ) => {
72- const index = model .value .indexOf ( file)
77+ const index = model .value .findIndex ( item => item . name === file . name )
7378 if (index > - 1 ) {
7479 model .value .splice (index, 1 )
7580 fileList .value = model .value
8489 fullscreenLoading .value = false
8590 emits (' on-error' , err)
8691 }
92+
93+ const checkFile = (file ) => {
94+ fullscreenLoading .value = true
95+ const isFileSize = file .size / 1024 / 1024 < props .fileSize // 文件大小
96+ let pass = true
97+ if (! isFileSize) {
98+ ElMessage .error (` 上传文件大小不能超过 ${ props .fileSize } MB` )
99+ fullscreenLoading .value = false
100+ pass = false
101+ }
102+
103+ // 获取accept属性
104+ const acceptPattern = props .accept
105+ if (acceptPattern) {
106+ // 将accept字符串转换为正则表达式进行验证
107+ // 例如: "image/*" 需要转换为匹配所有image类型
108+ const pattern = new RegExp (
109+ acceptPattern
110+ .split (' ,' )
111+ .map (type => type .trim ().replace (' *' , ' .*' ))
112+ .join (' |' )
113+ )
114+
115+ if (! pattern .test (file .type )) {
116+ ElMessage .error (` 请上传${ acceptPattern} 格式的文件` )
117+ fullscreenLoading .value = false
118+ pass = false
119+ }
120+ }
121+
122+
123+ console .log (' upload file check result: ' , pass)
124+
125+ return pass
126+ }
87127 </script >
Original file line number Diff line number Diff line change @@ -85,6 +85,7 @@ import { VueCropper } from 'vue-cropper'
8585import { getBaseUrl } from ' @/utils/format'
8686import { useRouter } from ' vue-router'
8787import { useUserStore } from " @/pinia" ;
88+ import {isImageMime } from " @/utils/image.js" ;
8889
8990defineOptions ({
9091 name: ' scanUpload'
@@ -138,9 +139,9 @@ const fixedRatio = ref(false)
138139
139140// 文件处理
140141const handleFileChange = (file ) => {
141- const isImage = file .raw . type . includes ( ' image ' )
142+ const isImage = isImageMime ( file .type )
142143 if (! isImage) {
143- ElMessage .error (' 请选择图片文件 ' )
144+ ElMessage .error (' 上传图片只能是 jpg、png、svg、webp 格式 ' )
144145 return
145146 }
146147
You can’t perform that action at this time.
0 commit comments