博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Angular] Provide Feedback to Progress Events with Angular’s HttpRequest Object
阅读量:4560 次
发布时间:2019-06-08

本文共 1484 字,大约阅读时间需要 4 分钟。

In some cases your application might need to upload large amounts of data, such as files. Obviously for a good UX we should provide the user some feedback on the progress of the upload. Angular’s HttpRequest object has a property reportProgress which allows us to do exactly that. Let’s see how.

 

// service:import { Injectable } from '@angular/core';import { Observable } from 'rxjs/Observable';import { HttpClient, HttpRequest, HttpEvent } from '@angular/common/http';export interface Person {  name: string;}@Injectable()export class PeopleService {  constructor(private http: HttpClient) {}  uploadAvatar(data): Observable
> { const req = new HttpRequest( 'POST', 'https://reqres.in/api/users/1', data, { reportProgress: true } ); return this.http.request(req); }}

 

// Component  import { HttpClient, HttpRequest, HttpEvent, HttpEventType } from '@angular/common/http';  uploadAvatar(fileUpload) {    const formData = new FormData();    formData.append('avatar', fileUpload.files[0], 'avatar.jpg');    this.peopleService      .uploadAvatar(formData)      .subscribe(res => {        if (res.type === HttpEventType.UploadProgress) {          const percentage = Math.round(100 * res.loaded / res.total);          this.output = `File is ${percentage}% uploaded`;        } else if (res instanceof HttpResponse) {          this.output = `File is completely uploaded`;        }      });  }

 

 

 

转载于:https://www.cnblogs.com/Answer1215/p/8448909.html

你可能感兴趣的文章
SQL 设置自增,和default
查看>>
hadoop测试题目-每天5题,总35题,第四天
查看>>
锐捷Linux版的下载和使用(福大客户端)
查看>>
半年总结——欲戴王冠,必承其重
查看>>
HDU Today HDU杭电2112【Dijkstra || SPFA】
查看>>
centos7下yum安装mysql
查看>>
在java中如何将字符型数组转换到字符串
查看>>
微信小项目
查看>>
Linux查看文件编码格式及文件编码转换
查看>>
Docker最全教程之使用 Visual Studio Code玩转Docker(二十)
查看>>
office远程代码执行(CVE-2017-11882)
查看>>
耿建超英语语法---状语从句
查看>>
【bzoj2400】Spoj 839 Optimal Marks 网络流最小割
查看>>
Git学习教程一之远程仓库
查看>>
[原]逆向iOS SDK -- _UIImageAtPath 的实现(SDK 6.1)
查看>>
【学习笔记】分类算法-逻辑回归
查看>>
vue单文件 style important引入样式
查看>>
输入三个数,打印出中间值(即第二大值)
查看>>
学习excel的使用技巧四显示正常的数字
查看>>
正则表达式要匹配双引号"如何才能匹配
查看>>