我有一个输入类型文件
<input type="file" class="userUploadButton" name="image" accept="image/*" on-change={this.setImage}/>
Run Code Online (Sandbox Code Playgroud)
和 Vue - 方法“setImage”
setImage(e){
const file = e.target.files[0];
if (!file.type.includes('image/')) {
Vue.swal({
title: 'Error!',
text: 'This is no image',
type: 'error',
});
return;
}
if(typeof FileReader === 'function'){
const reader = new FileReader();
reader.onload = (event) => {
this.imgSrc = event.target.result;
this.$refs.cropper.replace(event.target.result);
};
reader.readAsDataURL(file);
}else{
Vue.swal({
title: 'Error',
text: 'Your browser does not support FileReader API',
type: 'error',
});
}
},
Run Code Online (Sandbox Code Playgroud)
在用户上传图像的那一刻,我必须检查该图像的宽度和高度并停止上传(或删除图像)
实际上,文件只是一个文件,您需要使用new Image()文件源创建一个图像。
使用以下源代码
var width, height;
var _URL = window.URL || window.webkitURL;
img = new Image();
img.onload = function() {
// here you got the width and height
width = this.width;
height = this.height;
};
img.onerror = function() {
alert( "not a valid file: " + file.type);
};
img.src = _URL.createObjectURL(file);
Run Code Online (Sandbox Code Playgroud)
希望这对你有帮助!!
| 归档时间: |
|
| 查看次数: |
8547 次 |
| 最近记录: |