element 上传图片限制图片尺寸大小以及格式,贴源码,写的很清楚,已优化

<el-upload
            class="avatar-uploader"
            action="这里不需要填或者随便写"
            list-type="picture-card"
            :before-upload="beforeAvatarUpload"
            :on-preview="handlePictureCardPreview"
            :http-request="beforeMasterPictureUpload"
            :on-change="onChangeMaster"
            :on-exceed="OnExceed"
            :file-list="fileList"
            :limit="1"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>

上面是标签 下面是方法

下面是以前的旧方法,经过优化后可往后翻

// 主图上传成功
    handleMasterPictureSuccess(res, file) {
          this.dialogImageUrl = URL.createObjectURL(file.raw);
          console.log(this.dialogImageUrl)
          if (this.dialogImageUrl) {
            // this.$message('上传成功');
            this.$message({
          message: this.$t('Sellerbanner.Sellerbanner.Uploadsuccess'),
          type: 'success'
        });
          }
    },
    // 图片触发
    onChangeMaster(file, fileList){
         let existFile = fileList.slice(0, fileList.length - 1).find(f => f.name === file.name)
          if (existFile) {
                this.$utils.commonUtils.tip(this.$t('Sellerbanner.Sellerbanner.currentfile'),'error')
            fileList.pop()
          }
         this.fileList = fileList
    },
    // 主图上传之前
    beforeMasterPictureUpload(file) {
      // console.log(file)
      let param = new FormData(); //创建form对象
      this.fileList.forEach(file => { 
            param.append('files', file.raw, file.raw.name); //此处一定是append file.raw 上传文件只需维护fileList file.raw.name要加上 
    }) 
      let config = {   //添加请求头
          headers:{'Content-Type':'multipart/form-data'}
      };
      // this.$api.generalize.uploading(param,config).then( res => {
      // const {data} = res;
      // this.dialogImageUrl = this.$utils.goodUtils.setObjSplit(data); // 主图 分隔符 | 格式传参
      // })

      const isJPG = file.type === 'image/jpeg';
      const isGIF = file.type === 'image/gif';
      const isPNG = file.type === 'image/png';
      const isBMP = file.type === 'image/bmp';
      const isname = new RegExp("[\u4E00-\u9FA5]+");
      if (isname.test(file.name)) {
        this.$message.error(this.$t('Sellerbanner.Sellerbanner.beChinese'));
            return false
          }
          let _this = this
          if (file.type.indexOf('image')<0) {
            this.$message.error(this.$t('Sellerbanner.Sellerbanner.isincorrect'));
            return false
          }
          if (!isJPG && !isGIF && !isPNG && !isBMP) {
          this.$message.error(this.$t('Sellerbanner.Sellerbanner.Uploadingimages'));
          }
      let a = this.bannerCur;
      switch(a){
        case '1':
        {
          if (this.bannerCur =&#
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值