上传前校验图片的宽高

checkWH(file) {
        console.log(this.info.templateName,
          'templateNametemplateNametemplateNametemplateName');

        if (this.info.templateName == 1) {
          return this.checkImageWH(file, '750', '1696')
        } else {
          return this.checkImageWH(file, '750', '1624')
        }

      },
      checkImageWH(file, width, height) {
        let self = this;
        return new Promise(function(resolve, reject) {
          let filereader = new FileReader();
          filereader.onload = e => {
            let src = e.target.result;
            const image = new Image();
            image.onload = function() {
              console.log(this.width, this.height, width, height,
                'this.widththis.widththis.widththis.width');
              if (width && this.width != width && height && this.height != height) {
                self.$Message.error(`请上传宽为${width}px,高为${height}px的图片`);
                reject(false);
              } {
                resolve(true);
              }
            };
            image.onerror = reject;
            image.src = src;
          };
          filereader.readAsDataURL(file);
        });
      },

 

posted @ 2022-02-17 13:41  张小中  阅读(80)  评论(0编辑  收藏  举报