需求判断上传的类型只能是图片
const fileType = file.type.substring(
file.type.lastIndexOf("/") + 1,
file.type.length
);
let type =
fileType == "jpg" ||
fileType == "jpeg" ||
fileType == "png" ||
fileType == "JPG" ||
fileType == "JPEG";
console.log("type", type);
if (!type) {
_this.$message.error(
"上传图片只能是jpg 、 jpeg 、 png 、 JPG 、 JPEG格式!"
);
return false;
}
上传的大小不能超过500KB
const { xhr, uploadUrl, file, fileName } = fileLoader;
const isLt5KB = file.size / 1024 < 500;
if (!isLt5KB) {
_this.$message.error("上传失败,图片不可超过500KB!");
return false;
}
如果不能超过1MB
const isLt1M = file.size / 1024 / 1024 < 1;
console.log("file.size", file.size, isLt1M);
if (!isLt1M) {
_this.$message.error("上传失败,图片不可超过1MB!");
return false;
}
————————————————
版权声明:本文为CSDN博主「周家大小姐.」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_40190624/article/details/125309074