ng-zorro-antd 实现文件流上传,获取上传文件的路径

HTML页面

<nz-upload 
        nzAccept="application/pdf,image/jpeg,image/jpg,image/png"
        [(nzFileList)]="fileList"
        [(nzAction)]="UPLOAD_FILE"
        [nzShowButton]="fileList.length < 1"
        [nzBeforeUpload]="beforeUpload"
        (nzChange)="getFileUrl($event)" >
        <button nz-button>
            <i nz-icon nzType="upload"></i>
            <span>上传文件</span>            
        </button>
</nz-upload>

ts文件

 import { Component, OnInit } from '@angular/core';
 import { NzMessageService, NzModalService, UploadFile } from 'ng-zorro-antd';
    
 @Component({
     selector: 'app-detail',
     templateUrl: './detail.component.html',
     styleUrls: ['./detail.component.scss']
 })
 export class DetailComponent implements OnInit {
  	UPLOAD_FILE = ''; // 文件上传路径
    constructor(
         private activeRoute: ActivatedRoute,
         private msg: NzMessageService,
         private router: Router,
         private message: NzModalService,
    ){ }
              
    fileList: UploadFile[] = [];
  	beforeUpload = (file: UploadFile): boolean => {
         // 对上传文件类型进行判断
         const type = file.type;
               
         const str = ['application/pdf', 'image/jpg', 'image/jpeg', 'image/png'];
              if (str.indexOf(type) < 0) {
                  this.message.warning({
                      nzTitle: '提示',
                      nzContent: '选择文件失败,仅支持pdf、jpg、jpeg、png等格式的文件'
                  });
                  return false;
              }
              // 对上传文件大小进行限制
              const isLt20M = file.size / 1024 / 1024 < 30;
              if (!isLt20M) {
                  this.message.warning({
                      nzTitle: '提示',
                      nzContent: '文件必须在30M以内'
                  });
                  return false;
              }
              this.fileList = this.fileList.concat(file);
              // 当类型与大小都符合要求,直接上传;如return false,则需手动调用上传方法
              return true;
          }
          //文件上传完成获取路径的方法     	 
      getFileUrl({ file, fileList }): void {
              const status = file.status;
              if (status === 'done') {
                this.zizhi_prove = file.response.data
              } else if (status === 'error') {
                this.msg.error(`${file.name} file upload failed.`);
              }
        }
  	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值