关于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");
  }
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理