乐趣区

关于formdata:vue-上传图片判断大小尺寸

uploadImg(e, record, index) {const file = e.target.files[0];
  // 判断上传图片的大小 限度
  if (file.size / 1024 < 1000) {
    const that = this;
    let imgWidth = "";
    let imgHight = "";
    // 限度图片的尺寸 为 2000*1500
    const isSize = new Promise(function(resolve, reject) {
      const _URL = window.URL || window.webkitURL;
      const img = new Image();
      img.onload = function() {
        imgWidth = img.width;
        imgHight = img.height;
        const valid =
          img.width === parseInt(2000) && img.height === parseInt(1500);
        valid ? resolve() : reject();
      };
      img.src = _URL.createObjectURL(file);
    }).then(() => {const formData = new FormData();
        formData.append("files", file);
        that.loading = true;
        // 这里是调取接口
        接口名字 (formData)
          .then(res => {
              // 接口胜利操作啥的
              that.$message.success("图片上传胜利");
    
          })
          .finally(() => {that.loading = false;});
      },
      () => {
        // 图片尺寸不对 提醒
        this.$message.info(` 图片尺寸应为 2000x1500, 以后图片尺寸为 ${imgWidth}x${imgHight}`
        );
        return Promise.reject();}
    );
  } else {this.$message.info("图片最大 1000k");
  }
}
退出移动版