要点:
1.上传之前的的钩子函数
2.利用promise返回图片加载的状态
代码如下:
beforeAvatarUpload (res, file) {
const isSize = new Promise((resolve, reject) => {
let width = 220
let height = 220
let _URL = window.URL || window.webkitURL
let img = new Image()
img.onload = function () {
let valid = img.width === width && img.height === height
// eslint-disable-next-line prefer-promise-reject-errors
valid ? resolve() : reject()
}
img.src = _URL.createObjectURL(res)
}).then(() => {
return res
}, () => {
this.$message.error('上传的图片必须是等于或大于220*220!')
// eslint-disable-next-line prefer-promise-reject-errors
return Promise.reject()
})
return isSize
},