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
热门推荐
山东:首期退役士兵教师资格技能培训班圆满落幕
车牌号查询车辆信息的法律限制有哪些
同寝室的她们拿下世界名校offer,回国后计划……
香蕉的秘密身份:酸碱属性大揭秘与食用艺术
提升生物多样性,上海的野生动物栖息地建设成效显著
Nat. Rev. Neurosci.综述:结构化信息驱动的有向脑连接模型
浴室设计指南:从材料选择到空间布局,打造理想洗浴空间
如何强化制度的约束力和执行力
不结婚和结婚的差别:一个选择自由,一个选择责任
布洛芬过敏怎么办?常见的退烧药有哪些?
象棋车马炮组杀技巧详解:从基础到实战的全面指南
中国历朝历代各多少年
耳鳴怎麼辦?耳鼻喉科醫師解析原因、治療、解決方法
Web浏览器如何打开微信链接
一图对比 | 这么多种布洛芬,哪种效果好?
臀肌挛缩症保守治疗方法
幼儿园“班级管理”不可或缺的8步骤
开源技术引领AI时代——2024开放原子开发者大会深度解析
云南白药胶囊对什么疾病有效和成分是什么
Windows 11系统版本查看方法
5种分红领取方式,选错收益相差60%!
青少年减肥方法注意这4点:每天减少200-500卡路里的摄入
提升竞争力:制造业降本增效的5大方法
Excel制作工作总结:从数据整理到自动化分析的完整指南
文学创作:从现实到虚构的艺术探索
探秘生命的意义:从故事与哲学看世界真相
一节好的美术课如何体现?关键要点一览无遗!
盖世太保:纳粹德国的秘密警察与恐怖统治
香港故事|科技时钟:一键穿梭香港“城市景昔”
制冷机组常见制冷剂特性与优缺点