最近的项目中有需要限制上传图片的尺寸:
<form id="uploadForm">
<input type="file" id="file" @change="uploadImg($event)">
</form>
方法如下:
uploadImg(event) { //上传图片
const _this = this;
let _file = event.target.files[0];
let reader = new FileReader();
reader.readAsDataURL(_file);
reader.onload = function(e) {
let image = new Image();
image.src = e.target.result;
image.onload = function() {
let w = this.width;
let h = this.height;
if (w !== 720 || h !== 360) {
_this.$message.info('请上传尺寸为720px*360px的图片');
return false;
} else {
//上传成功
//...
}
}
}
}