nz-upload的[nzCustomRequest]自定义上传

场景:上传文件,上传后还可点击编辑弹出模态框重新上传,使用ng-zorro-antd库开发,运用nz-upload组件的[nzCustomRequest]自定义上传。

Bug[nzCustomRequest]自定义上传接口调通后大概是由于接口返回的数据和组件封装所需的数据格式不符的原因导致:**上传的文件列表的status始终处于loading状态,并且上传进度始终为0。**如下图:
在这里插入图片描述
最初解决方案,为所欲为的瞎用(不推荐)

1)解决一直loading的思路来源于文档的中的(nzChange),因为他的回调中包含status字段,如下图:
在这里插入图片描述
通过nz-upload组件绑定(nzChange)事件,方法中强行修改文件见列表的status值,代码如下:

fileChage(file){
	file.file.status='done';  // 改变文件列表中文件的状态,强行阻止一直loading的问题
}

2)进度条始终为0的问题,也是看到官方文档有相应的方法,才瞎搞的,如下图:
在这里插入图片描述
代码如下:

 // 上传
  firmwareFileCustomRequest = (file: any) => {
      const fd = new FormData();
      fd.append("file", file.file as any);
      this.equiment.postFirmwareUpload(fd).subscribe(
        (event: HttpEvent<{}>) => {
          (event as any).percent = 100;  // 进度条的值直接设置为100
          // tslint:disable-next-line:no-non-null-assertion
          file.onProgress!(event, file.file!);  // 进度事件回调
          //  可以解决问题,但不推荐。
        },
        err => {
          // tslint:disable-next-line:no-non-null-assertion
          file.onError!(err, file.file!);
        }
      );
  }

最终解决方案(推荐)
通过[nzBeforeUpload]给文件列表赋值,然后自定义上传[nzCustomRequest]中正常上传即可。
完整代码如下

        <nz-upload id='firmwareFile' [nzCustomRequest]="firmwareFileCustomRequest" [nzFileList]='firmwareFileList'
          [nzBeforeUpload]="beforeUpload" nzAccept=".crt" [nzRemove]="firmwareFileRemove">
          <button nz-button><i nz-icon nzType="upload"></i>{{'app.equipment.SelectFirmware' | translate}}</button>
        </nz-upload>
import { NzUploadFile} from 'ng-zorro-antd/upload';
import { HttpEvent } from '@angular/common/http';

export class TboxManagerComponent implements OnInit {
  // ....
  firmwareFileList: NzUploadFile[] = [];   // 上传文件列表
  // ....
  // ....
   beforeUpload = (file: NzUploadFile): any => {
    this.firmwareFileList = this.firmwareFileList.concat(file);
    // return false;
  }
  // 上传
  firmwareFileCustomRequest = (file: any) => {
      const fd = new FormData();
      fd.append("file", file.file as any);
      this.equiment.postFirmwareUpload(fd).subscribe(
        (event: HttpEvent<{}>) => {
            // ...
			// 成功后的一些逻辑操作操作
			// ...
        },
        err => {
          // tslint:disable-next-line:no-non-null-assertion
          file.onError!(err, file.file!);
        }
      );
    }
  }
}

点赞、关注加收藏,我们一起学前端📚

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
在使用 TypeScript 编写上述表单时,你可以这样写: ```typescript import { Component } from '@angular/core'; @Component({ selector: 'app-search-form', templateUrl: './search-form.component.html', styleUrls: ['./search-form.component.css'] }) export class SearchFormComponent { q = { ajmc: '', ajbh: '', }; onSubmit() { // 处理表单提交事件 } onReset() { this.q.ajmc = ''; this.q.ajbh = ''; } } ``` 在这个例子中,我们使用了 Angular 框架来构建组件。在组件类中,我们定义了一个 `q` 对象来存储表单中的数据。当提交表单时,我们可以在 `onSubmit` 方法中处理表单提交事件,当重置表单时,我们可以在 `onReset` 方法中将 `q` 对象中的属性值设置为空字符串,从而实现表单重置的功能。 在 HTML 文件中,你可以这样写: ```html <form nz-form [nzLayout]="'inline'" class="search-form" (ngSubmit)="onSubmit()"> <nz-row> <nz-col> <nz-form-item> <nz-form-label>  案件名称</nz-form-label> <nz-form-control> <nz-input-group> <input nz-input width="200px" [(ngModel)]="q.ajmc" name="ajmc" placeholder="请输入案件名称"> </nz-input-group> </nz-form-control> </nz-form-item> </nz-col> <nz-col> <nz-form-item> <nz-form-label>案件编号</nz-form-label> <nz-form-control> <nz-input-group> <input nz-input width="200px" [(ngModel)]="q.ajbh" name="ajbh" placeholder="请输入案件编号"> </nz-input-group> </nz-form-control> </nz-form-item> </nz-col> <nz-col> <button style="margin-top: 3px;" nz-button type="submit" [nzType]="'primary'">查询</button> <button nz-button type="button" class="mx-sm" (click)="onReset()">重置</button> </nz-col> </nz-row> </form> ``` 在这个例子中,我们使用 `(ngSubmit)` 指令来监听表单的提交事件,并在事件发生时调用组件类中的 `onSubmit` 方法。在重置按钮中,我们使用 `(click)` 指令来监听按钮的点击事件,并在事件发生时调用组件类中的 `onReset` 方法。注意,重置按钮的类型应该为 `type="button"`,这样可以避免表单的提交事件被触发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值