1,先在el-upload设置accept属性,限定文件类型
// 上传文件之前对文件大小和类型做限制,官方自带accept属性还是可以选中全部文件 beforeUpload(file) { let types = ['image/jpeg', 'image/jpg', 'image/gif', 'image/bmp', 'image/png']; const isImage = types.includes(file.type); const isLtSize = file.size / 1024 / 1024 < 2; if (!isImage) { this.$message.error('上传图片只能是 JPG、JPEG、gif、bmp、PNG 格式!'); return false; } if (!isLtSize) { this.$message.error('上传图片大小不能超过 2MB!'); return false; } return true; },