当前位置: 首页 > news >正文

处理文件上传和进度条的显示(进度条随文件上传进度值变化)

成品效果图:

解决问题:上传文件过大时,等待时间过长,但是进度条却不会动,只会在上传完成之后才会显示上传完成

上传文件的upload.component.html

<nz-modal [(nzVisible)]="isVisible" [nzTitle]="'文件上传'" [nzWidth]="'1000px'" [nzFooter]="modalFooter"(nzOnCancel)="onCancel()" class="advice-upload-file"><div nz-row><nz-upload #uploadListData nzType="drag" [(nzFileList)]="fileList" [nzMultiple]="isMultiple" [nzLimit]="0"[nzBeforeUpload]="beforeUpload"><p class="ant-upload-drag-icon"><i nz-icon nzType="inbox"></i></p><p>点击或拖拽上传</p></nz-upload><div style="padding-top: 10px"><h6>文件上传进度:</h6><nz-progress [nzPercent]="percent"></nz-progress></div></div><ng-template #modalFooter><button nz-button nzType="default" (click)="cleanList()">清空上传队列</button><button nz-button nzType="default" (click)="onCancel()">取消</button><button nz-button nzType="primary" (click)="submit()">上传</button></ng-template>
</nz-modal>

upload.component.ts

  /** 是否允许上传多个文件 */isMultiple = true;/*** 文件赋值列表*/fileList = [];/*** 上传进度条*/percent = null;/** 上传 */submit() {let successCount = 0;this.fileList.forEach(file => {const formData: FormData = new FormData();formData.append('file', file.originFileObj, file.name);this.uploadService.uploadMultiFiles(formData, this.categoryId, file.name).subscribe(data => {if (data) {successCount++;this.msg.create('success', data['fileName'] + `上传成功!`);this.percent = Number((successCount / this.fileList.length * 100).toFixed(2));}if (this.fileList.length === successCount) {setTimeout(() => {this.onCancel();this.notification.emit({operation: null,data: null});}, 1000);}});});}cleanList() {this.fileList = [];}beforeUpload = (file: UploadFile) => {// const isLt200M = file.size / 1024 / 1024 < 200;// if (!isLt200M) {//   this.msg.error('文件大小不超过200MB!');//   return false;// }return true;}

接口:

/** 上传文件 */uploadMultiFiles(files: FormData, categoryId: string, fileName: string): Observable<Array<any>> {return this.http.post(`${this.URL}` + `/uploadFile?fileName=${fileName}&categoryId=${categoryId}`, files);}

效果就是上传文件大时进度条一直是0%,然后上传完成才100%(会让用户误解没上传成功重复上传)

效果图:

解决方法如下:

1、修改接口里面的传参post,鼠标移上去一般有显示类型参数

  /** 上传文件 */uploadMultiFiles(files: FormData, categoryId: string, fileName: string): Observable<any> {return this.http.post(`${this.URL}` + `/uploadFile?fileName=${fileName}&categoryId=${categoryId}`, files, {}, {reportProgress: true,observe: 'events',});}

2、修改upload.component.ts 文件的提交方法

  /** 上传 */submit() {this.fileList.forEach(file => {const formData: FormData = new FormData();formData.append('file', file.originFileObj, file.name);this.uploadService.uploadMultiFiles(formData, this.categoryId, file.name).subscribe(event => {if (event.type === HttpEventType.UploadProgress) {this.percent = Math.round(100 * event.loaded / event.total);} else if (event.type === HttpEventType.Response) {// 文件上传成功this.msg.create('success', event.body['fileName'] + `上传成功!`);setTimeout(() => {this.onCancel();this.notification.emit({operation: null,data: null});}, 1000);}});});}

效果图:进度值会随着上传多少变化

参考文章:

angular:

https://www.yisu.com/jc/843309.html

axios:

https://www.jianshu.com/p/9564b549d2d6

http://www.lryc.cn/news/468500.html

相关文章:

  • 【套题】大沥2019年真题——第5题
  • 上传Gitee仓库流程图
  • 二叉树相关OJ题 — 第一弹
  • 【学习笔记】RFID
  • 自动化部署-01-jenkins安装
  • AI工具大爆发,建议每个都使用收藏
  • Mybatis之参数处理
  • windows内核探索--打印windows的GDT表(全局描述符表)
  • 【ChatGPT】让ChatGPT帮助进行头脑风暴与创意生成
  • 大数据处理随堂测试
  • 2024最新pycharm安装教程及基本使用(超详细,新手小白必看)
  • 三国杀钓鱼自动化
  • 在pycharm中使用sqllite
  • Linux——文件操作
  • 数据结构 ——— 数组栈oj题:有效括号
  • Character AI被起诉!14岁青少年自杀,AI陪伴何去何从
  • CSS3 动画相关属性实例大全(三)(columns、filter、flex、flex-basis 、flex-grow、flex-shrink属性)
  • 中国最厉害的思想家改名大师颜廷利:以诚信为基,塑企业信誉
  • Python 代码实现用于进行水质模拟和优化加氯量
  • 挖矿病毒来势汹汹
  • 国产数据库的蓝海在哪?
  • MySQL~表的操作(创建表,查看表,修改表,删除表)
  • 多线程加锁与手搓智能指针实践
  • 3180. 执行操作可获得的最大总奖励 I
  • react18中的jsx 底层渲染机制相关原理
  • Spring Boot 实现文件上传下载功能
  • ArcGIS 10.8 安装教程(含安装包)
  • 【小白学机器学习16】 概率论的世界观2: 从正态分布去认识世界
  • Python 爬虫项目实战:爬取某云热歌榜歌曲
  • HCIP-HarmonyOS Application Developer 习题(十八)