beforeUpload(file) {
// 上传图片前
const isJPG = this.fileType(file);
if (!isJPG) {
this.$message({
message: "请上传jpg,png,jpeg格式的图片",
type: "error"
});
}
const isLt5M = file.size / 1024 / 1024 < 5;
if (!isLt5M) {
this.$message({
message: "文件不能大于5M!",
type: "error"
});
}
const isSize = new Promise(function (resolve, reject) {
let width = 375;
let height = 150;
let _URL = window.URL || window.webkitURL;
let img = new Image();
img.onload = function () {
console.log(img.width, img.height, "cccc");
let valid = img.width == width && img.height == height;
valid ? resolve() : reject();
}
img.src = _URL.createObjectURL(file);
}).then(() => {
return file;
}, () => {
this.$message({
message: '请上传尺寸为375*150的图片!',
type: 'error'
});
return Promise.reject()
return false;//必须加上return false; 才能阻止
})
return isLt5M && isSize;
}
element upload限制图片宽高
最新推荐文章于 2024-07-04 19:22:03 发布