1,图片校验规则
(1), 上传图片前使用before-upload函数,自定义函数名:
使用规则
然后使用定义函数规则:
其中,有检验图片名称的正则(图片命名不允许出现空格和汉族),如果不符合返回false,即图片选择失败切页面不回显选中图片,代码如下:
const reg = /[\s]|[\u4e00-\u9fa5]/g
if (reg.test(file.name)) {
this.$message.error('图片路径中禁止使用中文汉字或空格')
return false
}
有检验图片格式和类型的的方法:
if (file && file.name) {
const isTYPE = file.type === 'image/png' || file.type === 'image/jpg' || file.type === 'image/jpeg' || file.type === 'image/bmp'
if (!isTYPE) {
this.$message.warning('上传头像只能是png/bmp/jpeg/jpg格式!')
return isTYPE
}
this.fileData.name = file.name
return isTYPE
}
(2), 另外一种方式为上传方法的校验
其中,下面两行代码为关键:可自行复制
const idx = this.$refs.appletPic.uploadFiles.findIndex(item => item.uid === params.file.uid)
this.$refs.appletPic.uploadFiles.splice(idx, 1)