在Vue项目里,利用Iview的upload组件,上传图片,在图片上传前,判断图片尺寸

     handleBeforeUpload(file) {
      let _this = this
      return new Promise((resolve, reject) => {
        const check = _this.uploadList.length < _this.mutileUploadData.maxNum
        if (!check) {
          this.$Notice.warning({
            title: 最多上传 + this.mutileUploadData.maxNum + 
          })
          reject(false)//重点
        }
        debugger
        let image = new Image()
        image.src = URL.createObjectURL(file)
        image.onload = () => {
          let width = _this.mutileUploadData.maxWidth
          let height = _this.mutileUploadData.maxHeight
          if (
            (width && image.width > width) ||
            (height && image.height > height)
          ) {
            this.$Modal.warning({
              title: 图片尺寸,
              content:
                图片尺寸  +
                file.name +
                 太大,不能超过 +
                this.mutileUploadData.maxWidth +
                * +
                this.mutileUploadData.maxHeight +
                ,
              onOk() {}
            })
            reject(false)//重点
          }
          resolve(true)//重点
        }
        image.onerror = () => {
          reject()
        }
      })
    }

为什么要用Promise呢,因为image.onload()方法异步,

在Vue项目里,利用Iview的upload组件,上传图片,在图片上传前,判断图片尺寸

上一篇:vue-axios中post和get携带参数和token


下一篇:S7.Net与西门子PLC通讯——纯新手必看