beforeUpload(file) {
const isJPG = file.type === "image/png"; // 验证图片类型
const isLt100M = file.size / (1024 * 1024) < 100; // 验证图片大小
const isSize = new Promise(function (resolve, reject) {
let width = 284; // 限制上传图片的宽度
let height = 213; // 限制上传图片的高度
let URL = window.URL || window.webkitURL;
let image = new Image();
image.onload = function () {
let valid = image.width <= width && image.height <= height;
valid ? resolve() : reject();
};
image.src = URL.createObjectURL(file);
}).then(
() => file,
() => {
this.$message.error("图片尺寸过大,请上传284*213尺寸的图片"); // 提示 看需求
return Promise.reject();
}
);
if (!isJPG) {
this.$message.error("格式错误,请上传png格式图片"); // 提示 看需求
}
if (!isLt100M) {
this.$message.error("文件过大,文件大小不超过100M"); // 提示 看需求
}
return isJPG && isLt100M && isSize;
},
element 使用Upload组件 验证上传文件的大小 尺寸 类型
最新推荐文章于 2024-10-22 15:31:19 发布