vue 上传图片限制宽高
//上传之前beforeUpload(file) {const isJPG = file.type === 'image/jpeg' || file.type === 'image/gif'|| file.type === 'image/jpg' || file.type === 'image/png'|| file.type ===...
·
//上传之前
beforeUpload(file) {
const isJPG = file.type === 'image/jpeg' || file.type === 'image/gif'
|| file.type === 'image/jpg' || file.type === 'image/png'
|| file.type === 'image/GIF' || file.type === 'image/JPG' || file.type === 'image/bmp';
// const isLt2M = file.size / 1024 / 1024 < 2;
if (!isJPG) {
this.$message.error('图片格式不正确!');
return false
}
//如果要验证宽高
//this.validWidth 由上级组件传入 props 接收
//this.validHeight 由上级组件传入 props 接收
let validWidthAndHeight=true
if(this.validWidth&&this.validHeight){
return this.valWidthAndHeight(file)
}else return isJPG
},
//验证图片宽高
valWidthAndHeight:function(file){
let _this =this
return new Promise(function(resolve, reject) {
let width = _this.validWidth //图片宽度
let height = _this.validHeight ; //图片高度
let _URL = window.URL || window.webkitURL;
let image = new Image();
image.src = _URL.createObjectURL(file);
image.onload = function() {
let valid = image.width == width && image.height == height;
valid ? resolve() : reject();
};
}).then(
() => {
return file;
},
() => {
this.$message.error("上传图片尺寸不符合,只能是"+_this.validWidth+"*"+_this.validHeight+"!");
return Promise.reject();
}
);
}
更多推荐
已为社区贡献2条内容
所有评论(0)