限制upload组件上传图片大小
beforeUpload(file) {
this.uploadHeader.Authorization = "Bearer" + " " + local.get("t_k");
this.updataData.file = file.name;
this.updataData.fileType = file.type;
const isJpgPng = file.type === "image/jpeg" || file.type === "image/png";
if (!isJpgPng) {
this.$message({
message: "上传图片格式只能是jpg/png",
type: "warning",
});
return false;
}
const isLt2M = file.size / 1024 / 1024 < 1;
if (!isLt2M) {
this.$message({
message: "上传图片大小不能超过1M!请重新选择!",
type: "warning",
});
return false;
}
const isSize = new Promise(function (resolve, reject) {
let width = 160;
let height = 200;
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({
message: "上传图片尺寸只能是160*200px!请重新选择!",
type: "warning",
});
return Promise.reject();
}
);
return isJpgPng && isLt2M && isSize;
},