VUE antd Upload批量上传 (照片墙) 转base64方法

这段代码展示了如何处理图片上传组件的change事件,并在上传过程中进行类型和大小检查。当图片符合要求时,通过FileReader的readAsDataURL方法将文件转换为Base64编码,然后将结果存储到数据结构中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//文档里面的base64方法

function getBase64(file) {

  return new Promise((resolve, reject) => {

    const reader = new FileReader();

    reader.readAsDataURL(file);

    reader.onload = () => resolve(reader.result);

    reader.onerror = error => reject(error);

  });

}

//upload组件  change事件

    async uploadAvatarChange(info) {

      const isJpgOrPng =

        info.file.type === "image/jpeg" || info.file.type === "image/png";

      const isLt2M = info.file.size / 1024 / 1024 < 2;

      if (!isJpgOrPng) {

        this.$message.error("你只能上传jpg跟png类型的图片!");

      } else {

        if (!isLt2M) {

          this.$message.error("图片大小不能超过10MB!");

        } else {

          console.log(info);

          this.fileList = info.fileList;

          console.log(this.fileList);

          for (var i in info.fileList) {

            console.log(i);

            let picbaseobj = {};

            picbaseobj.type = info.fileList[i].type;

            console.log(getBase64(info.fileList[i].originFileObj));

            let base64arr = await getBase64(info.fileList[i].originFileObj);

            console.log(base64arr);

            picbaseobj.picBase64 = base64arr;

            this.picsBase64.push(Object.assign({}, picbaseobj));

            console.log(this.picsBase64);

          }

        }

      }

    },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值