最近的項目中有需要限制上傳圖片的尺寸:
<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 {
//上傳成功
//...
}
}
}
}