使用el-upload对上传文件或图片做限制时,before-upload方法不生效问题

使用el-upload对上传文件或图片做限制时,before-upload方法不生效问题

最近在公司项目中使用到了el-upload,但是在对上传的文件做大小限制或者格式限制时,发现before-upload方法并不生效,查阅资料后发现:因为 before-upload 是指在文件上传之前、文件已被选中,但还没上传的时候触发,而设置了 :auto-upload=“false” 后,文件上传事件不被再次调用,,所以 before-upload 不生效,所以,限制图片大小和格式的时候,需绑定在 :on-change 里面,这样限制就生效了。

<el-upload
      class="upload-demo"
      ref="upload"
      action
      :on-remove="handleRemove"
      :file-list="fileList"
      :auto-upload="false"
      :before-upload="beforeUpload"
      :on-change="handleChange"
    >
 handleChange(file,fileList){
      var FileExt=file.name.replace(/.+\./, "");
      if(['png','jpg','jpeg'].indexOf(FileExt.toLowerCase())===-1){
        this.$message({
          type:"warning",
          message:"请上传.png .jpg .jpeg格式的文件"
        });
        if(this.fileList.length<=0){
          this.fileList=[];
        }else{
          this.fileList=fileList.slice(0,1);
        }
        return false;
      }else if(this.isLt10M==='0'){
        this.$message({
          type:"warning",
          message:"上传文件的大小不能超过10M"
        });
        this.fileList=fileList.slice(0,1);
        return this.isLt10M==='1'?true:false;
      }else if(fileList.length>1){
        this.$confirm("文件已存在,是否确定替换文件",{
          confirmButtonText:"确定",
          cancelButtonText:"取消",
          showClose:false,
          type:"warning",
        })
        .then(()=>{
          this.fileList=fileList.slice(-1)
        })
        .catch(()=>{
          this.fileList=fileList.slice(0,1)
        })
      }else{
        this.fileList=fileList;
      }
    },
  • 9
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值