vue element-ui 限制文件大小不超过多少M

这篇博客介绍了在Vue项目中如何限制文件上传的大小和格式。通过使用`before-upload`和`on-change`两个事件,实现了在上传前检查文件大小是否超过5M以及文件类型是否为PDF或Excel。当文件大小超过限制或格式不符时,会显示相应的错误提示。此外,还提供了清理缓存和重启服务的解决无效问题的方法。
摘要由CSDN通过智能技术生成

目录

无效方式

有效方式

参考链接


注:

1、项目需求为10M,这里是以5M文件为案例,方便测试!

2、有时候改动不生效:(1)重启 npm run dev   (2)清理缓存  ctrl + shift + delete

            <el-upload
              :disabled="addEditDisabled.disable"
              class="upload-demo"
              :on-preview="handlePreview"
              style="display: inline"
              v-if="addEditDisabled.infoHide"
              drag
              :on-change="handleChange"
              :show-file-list="false"
              action="#"
              :multiple="false"
              :auto-upload="false"
              :before-upload="beforeAvatarUpload"
            >

无效方式

放 methods: {} 里面,:before-upload="beforeAvatarUpload"  无效!

    /** 文件大小不超过5M */
    beforeAvatarUpload(file){
      console.log("文件:::"+file)
      const isLtXM = file.size / 1024 / 1024 < 5;
      if(!isLtXM) {
        this.$message({
          message: '上传文件大小不能超过 5MB!',
          type: 'warning'
        });
      }
      return isLtXM
    },

有效方式

放 methods: {} 里面, :on-change="handleChange" 有效!

    handleChange(file, fileList) {
      if (!this.network) {
        this.open = false;
        return this.$message.warning(
          "当前网络已断开,请勿操作数据,否则可能导致数据异常。请等待网络恢复后再操作。"
        );
      }

      // 检查文件
      const fileName = file.name;
      const fileType = fileName
        .substring(fileName.lastIndexOf("."))
        .toLowerCase();
      if (fileType === ".pdf" || fileType === ".xls" || fileType === ".xlsx") {
        console.log("文件格式符合要求" + fileName);
      } else {
        fileList.splice(file, 1);
        return this.$message.error("附件格式仅支持PDF、Excel(xlsx、xls)");
      }

      const isLtXM = file.size / 1024 / 1024 < 5;
      if(!isLtXM) {
        console.log("文件大于5M");
        this.$message.error("上传文件大小不能超过 5MB!");
        return false;
      }else {
        console.log("文件小于5M");
      }

      if(this.allAttachmentNameArr.length > 9){
        return this.$message.error("最多只能上传10个附件")
      }
      for (let i = 0; i < this.allAttachmentNameArr.length; i++) {
        if (this.allAttachmentNameArr[i] == file.name) {
          fileList.splice(file, 1);
          return this.$message.error(
            "已存在相同文件名的合同附件,请修改附件名称后重新上传"
          );
        }
      }
      this.allAttachmentNameArr.push(fileName);

      let reader = new FileReader();

      let u = reader.readAsDataURL(file.raw);

      reader.onload = () => {
        console.log(reader.result);
      };

      this.newAttachmentList.push({
        uid: file.uid,
        name: file.name,
        url: file.url,
        raw: file.raw,
      });

      this.chageAttachment++;

    },

参考链接

element-ui 判断文件大小,文件大小不能超过2M_红孩儿2011的博客-CSDN博客_vue限制文件大小不超过2m

vue限制文件上传大小和上传格式 - I_Dreamer - 博客园

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

被开发耽误的大厨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值