直接上代码
组件给我们提供了图片上传的大小以及格式
拿来用即可,绑定事件
:before-upload="beforeAvatarUpload"
事件处理:
// 使用 before-upload 限制用户上传的图片格式和大小
beforeAvatarUpload(file) {
console.log('file',file)
const suffix = file.type === 'image/jpg' || file.type === 'image/png' || file.type === 'image/jpeg'
const isLt5M = file.size / 1024 / 1024 < 5;
if (!suffix) {
this.$message.error("上传头像图片只能是 JPG 格式!");
}
if (!isLt5M) {
this.$message.error("上传头像图片大小不能超过 5MB!");
}
return suffix && isLt5M;
},