// 判断尺寸是2:1,也可以限制固定尺寸
limitFileWH(file) {
const isSize = new Promise(function(resolve, reject) {
let _URL = window.URL || window.webkitURL;
let img = new Image();
img.onload = function() {
let valid = (img.width/img.height)%2 === 0;
valid ? resolve() : reject();
}
img.src = _URL.createObjectURL(file);
}).then(() => {
return true;
}, () => {
return false;
});
return isSize
},
// 同步请求
async myUpload(file) {
if (file.type) {
const isLt10M = file.size / 1024 / 1024 < 2
if (!isLt10M) {
this.$message.error('图片大小不能超过 10MB!')
return
}
const isImage = file.type
if (isImage !== 'image/jpeg' && isImage !== 'image/png' && isImage !== 'image/gif') {
this.$message.error('图片类型是jpg/png/gif')
return
}
var cj = true
await this.limitFileWH(file).then(res=>{
cj = res
})
if (!cj){
this.$message.error('尺寸必须是2:1')
return
}
}