el-upload组件实现多张图片格式和大小验证的详细教程
创作时间:
作者:
@小白创作中心
el-upload组件实现多张图片格式和大小验证的详细教程
引用
CSDN
1.
https://blog.csdn.net/babalamoxian/article/details/137822936
本文将详细介绍如何使用el-upload组件实现多张图片的验证功能,包括图片格式和大小的验证。文章内容较为详细,包含了需求说明、原理说明、HTML结构、JS结构以及函数解释等部分,并附有代码示例和效果截图。
需求
这是一个普通的图片上传框,最多可以上传8张图片。具体验证要求如下:
- 必填
- 不能超过5M(代码中定义变量写成isLt1000KB了,大家忽略)
- 格式必须是图片格式
- 验证错误提示‘第几张图片XX错误’
原理说明
对于一张图片的验证,基本原理如下:
const isLt1000KB = v.size / 1024 < 5000; //5M
const isPic =
/.*\.(bmp|jpg|png|tif|gif|pcx|tga|exif|fpx|svg|psd|cdr|pcd|dxf|ufo|eps|ai|raw|WMF|webp|jpeg)/i.test(
v.name
);
对于多张图片的验证,定义了一个Map来记录八张图片的情况。这样可以根据需要显示某张图片的验证结果。
const img2_err_msg = ref(
new Map([
[0, { isPic: true, isLt1000KB: true, file: null }],
[1, { isPic: true, isLt1000KB: true, file: null }],
[2, { isPic: true, isLt1000KB: true, file: null }],
[3, { isPic: true, isLt1000KB: true, file: null }],
[4, { isPic: true, isLt1000KB: true, file: null }],
[5, { isPic: true, isLt1000KB: true, file: null }],
[6, { isPic: true, isLt1000KB: true, file: null }],
[7, { isPic: true, isLt1000KB: true, file: null }],
])
);
HTML结构
<el-form
ref="refShopForm01"
:model="shopInfo"
:rules="rules"
...
>
...
<el-form-item class="m-32" label="室内图(最多8张)" prop="img2">
<el-upload
v-model:file-list="shopInfo.img2"
action="#"
list-type="picture-card"
:auto-upload="false"
:on-preview="handlePictureCardPreview"
:limit="8"
:on-remove="handleRemove2"
>
<el-icon><Plus /></el-icon>
</el-upload>
</el-form-item>
...
</el-form>
JS结构
const refShopForm01 = ref<FormInstance>();
//记录多张图片的不符合要求的问题
const img2_err_msg = ref(
new Map([
[0, { isPic: true, isLt1000KB: true, file: null }],
[1, { isPic: true, isLt1000KB: true, file: null }],
[2, { isPic: true, isLt1000KB: true, file: null }],
[3, { isPic: true, isLt1000KB: true, file: null }],
[4, { isPic: true, isLt1000KB: true, file: null }],
[5, { isPic: true, isLt1000KB: true, file: null }],
[6, { isPic: true, isLt1000KB: true, file: null }],
[7, { isPic: true, isLt1000KB: true, file: null }],
])
);
const shopInfo = reactive({
//室内图--最多8张
img2: [] as UploadUserFile[],
});
// 验证规则
const rules = reactive<FormRules>({
img2: [
{
required: true,
message: "请选择图片",
trigger: "blur",
},
{ validator: validatePic2, trigger: "blur" },
],
});
// 验证多张图片
function validatePic2(rule: any, value: UploadUserFile[], callback: any) {
let n = value.length;
if (n == 0) {
callback(new Error("请选择图片"));
} else {
_very(value[n - 1], n - 1);
}
function _very(v: UploadUserFile, i: number) {
const isLt1000KB = v.size / 1024 < 5000; //5M
const isPic =
/.*\.(bmp|jpg|png|tif|gif|pcx|tga|exif|fpx|svg|psd|cdr|pcd|dxf|ufo|eps|ai|raw|WMF|webp|jpeg)/i.test(
v.name
);
img2_err_msg.value.set(i, { isPic, isLt1000KB, file: v });
if (!isPic) {
callback(new Error(`第${i + 1}张-格式不支持`));
}
if (!isLt1000KB) {
callback(new Error(`第${i + 1}张-图片超过5M`));
}
if (isPic && isLt1000KB) {
let _msg = "";
img2_err_msg.value.forEach((item, index) => {
if (!item.isPic) {
_msg = `第${index + 1}张-格式不支持`;
return;
}
if (!item.isLt1000KB) {
_msg = `第${index + 1}张-图片超过5M`;
return;
}
});
if (_msg.length > 0) {
callback(_msg);
} else {
callback();
}
}
}
}
async function handleRemove2(
uploadFile: UploadFile,
uploadFiles: UploadUserFile[]
) {
let index = 7;
img2_err_msg.value.forEach((item, ii) => {
if (item.file == uploadFile) {
index = ii;
}
});
for (let ii = index; ii < 7; ii++) {
let _old = img2_err_msg.value.get(ii + 1);
img2_err_msg.value.set(ii, _old);
}
img2_err_msg.value.set(7, { isPic: true, isLt1000KB: true, file: null });
await refShopForm01.value?.validateField("img2");
}
JS函数解释
_very:验证某张图片是否通过验证handleRemove2:在移除图片的时候更新验证信息map,并重新验证
效果截图
- 正常图片
- 第二张超过5M
热门推荐
丹巴甲居藏寨:藏区童话世界的独特魅力
续写87年前《黄河大合唱》词、曲作者友谊佳话 两家后人来汉“同屏”抒怀
马德兴:国足热身赛塞鸟、王钰栋等首秀,张玉宁因小伤未出场
气血不足脾胃虚弱怎么办?全方位调理指南
数学教师就业和发展前景
大学生被逮捕后续处理的法律分析
小孩舌头上出现红点点怎么办?可能病因与治疗建议
《哈利·波特》全系列电影火热重映,最推荐看哪两部?
宝可梦传说阿尔宙斯新增特性详解(强力神秘力量的掌握者——阿尔宙斯)
亥姆霍兹线圈:原理、应用与技术细节
泰国药草和其他主要的材料
防治老年人腹泻,方法要对
警惕!这些坏习惯正在“偷走”你的听力
从用户体验视角拆解:如何专业拒绝“不匹配”候选人并提升雇主品牌
200米跑步呼吸技巧
电动汽车的电池容量一般有多少千瓦时?
尿毒症诊断标准
肺结节大小决定良性还是恶性,出现4类症状要注意,及时做好筛查
晚托班不签合同可以吗?法律解读与实务分析
如何评估律师打官司的收费标准
真正的孝道
特朗普币实时走势图深度解析:从暴涨到暴跌的幕后真相
硬盘AHCI驱动在Win11中怎么设置?为什么需要安装?
家里电脑IP地址怎么设置?详细指导
牛肉是温性的还是热性的
失恋后抑郁症怎么走出来
塑钢是什么材料?看完你就知道了!
什么是 Tokens?
TCP/IP协议配置与网络实用命令详解
使用ipconfig查看及刷新网络配置