vue ElementUI upload上传文件时对文件格式、大小和宽高的限制
beforeAvatarUpload(file) {const isJPG = file.type === 'image/jpg';const isPNG = file.type === 'image/png';const isLt20M = file.size / 10...
·
beforeAvatarUpload(file) {
const isJPG = file.type === 'image/jpg';
const isPNG = file.type === 'image/png';
const isLt20M = file.size / 1024 / 1024 < 20;
if (!isJPG) {
this.$message.error('上传图片的格式只能是 JPG或PNG 格式!');
}
if (!isPNG) {
this.$message.error('上传图片的格式只能是 JPG或PNG 格式!');
}
if (!isLt20M) {
this.$message.error('上传图片的大小不能超过 20M!');
}
const isSize = new Promise(function(resolve, reject) {
let width = 320;
let height = 320;
let _URL = window.URL || window.webkitURL;
let img = new Image();
img.onload = function() {
let valid = img.width == width && img.height == height;
valid ? resolve() : reject();
}
img.src = _URL.createObjectURL(file);
}).then(() => {
return file;
}, () => {
this.$message.error('上传的图片宽高必须是320*320!');
return Promise.reject();
});
return isPNG && isJPG && isSize && isLt20M;
},
更多推荐
已为社区贡献4条内容
所有评论(0)