ng.ant.design上传图片校验

ng.ant.design上传图片校验

html:

<div class="clearfix upload_containter">

              <nz-upload class='upload-btn' [nzCustomRequest]="customReq" [nzName]="'DrugImages'"

                [nzBeforeUpload]="beforeUpload" [(nzFileList)]="drugImagesList" nzListType="picture-card"

                [nzShowButton]="drugImagesList.length<5 && canEditor" [nzShowUploadList]="showUploadList"

                [nzPreview]="handlePreview" [nzRemove]='removeDrugImg'>

                <i nz-icon nzType="plus"></i>

                <div class="ant-upload-text">点击上传</div>

              </nz-upload>

            </div>

ts:

import { CheckImage } from '/shared/utils/CheckImage.base';

  /**

   * @description: 图片上传前校验

   * @params: {item: UploadXHRArgs}

   * @return:

   */

  beforeUpload = (file: File) => {

    return new Observable(observer => {

      const isJPG = file.type === 'image/jpeg';

      const isPNg = file.type === 'image/png';

      const isJPg = file.type === 'image/jpg';

      if (!isJPG && !isPNg && !isJPg) {

        this.nzMsg.error('只能上传jpeg,png,jpg格式图片');

        return;

      }

      const isLt1M = file.size / 1024 / 1024 < 1;

      if (!isLt1M) {

        this.nzMsg.error('图片大小只能小于 1MB!');

        return;

      }

      // check height

      CheckImage.checkImageDimension(file).then(dimensionRes => {

        if (!dimensionRes) {

          this.nzMsg.error('请上传大小为800*800px的图片');

          return;

        }

        // observer.next(isJPG && isLt1M && dimensionRes);

        // observer.complete();

        observer.next(file);

      });

    });

  };

CheckImage 文件

export class CheckImage {

  /**

   * @description: 校验上传图片大小

   * {file} 上传图片对象

   * @return: boolean

   */

  public static checkImageDimension(file: File): Promise<boolean> {

    return new Promise(resolve => {

      const img = new Image(); // create image

      img.src = window.URL.createObjectURL(file);

      img.onload = () => {

        const width = img.naturalWidth;

        const height = img.naturalHeight;

        window.URL.revokeObjectURL(img.src!);

        resolve(width === height && width === 800);

      };

    });

  }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值