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<HttpEvent<Object>> {
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`;
}
}); }

[Angular] Provide Feedback to Progress Events with Angular’s HttpRequest Object的更多相关文章

  1. 从Java角度理解Angular之入门篇:npm, yarn, Angular CLI

    本系列从Java程序员的角度,带大家理解前端Angular框架. 本文重点介绍Angular的开发.编译工具:npm, yarn, Angular CLI,它们就像Java在中的Maven,同时顺便介 ...

  2. Angular 个人深究(一)【Angular中的Typescript 装饰器】

    Angular 个人深究[Angular中的Typescript 装饰器] 最近进入一个新的前端项目,为了能够更好地了解Angular框架,想到要研究底层代码. 注:本人前端小白一枚,文章旨在记录自己 ...

  3. (转载)从Java角度理解Angular之入门篇:npm, yarn, Angular CLI

    本系列从Java程序员的角度,带大家理解前端Angular框架. 本文是入门篇.笔者认为亲自动手写代码做实验,是最有效最扎实的学习途径,而搭建开发环境是学习一门新技术最需要先学会的技能,是入门的前提. ...

  4. (转载) 上传文件进度事件,进度事件(Progress Events)

    转载URL:https://www.w3cmm.com/ajax/progress-events.html MDN参考:https://developer.mozilla.org/zh-CN/docs ...

  5. angular.module()创建、获取、注册angular中的模块

    // 传递参数不止一个,代表新建模块;空数组代表该模块不依赖其他模块 var createModule = angular.module("myModule", []); // 只 ...

  6. Angular入门,开发环境搭建,使用Angular CLI创建你的第一个Angular项目

    前言: 最近一直在使用阿里的NG-ZORRO(Angular组件库)开发公司后端的管理系统,写了一段时间的Angular以后发现对于我们.NET后端开发而言真是非常的友善.因此这篇文章主要是对这段时间 ...

  7. angular $digest already in progress

    angular.js:11706 Error: [$rootScope:inprog] $digest already in progresshttp://errors.angularjs.org/1 ...

  8. [Angular] Communicate with Angular Elements using Inputs and Events

    In a real world scenario we obviously need to be able to communicate with an Angular Element embedde ...

  9. [Angular Directive] 3. Handle Events with Angular 2 Directives

    A @Directive can also listen to events on their host element using @HostListener. This allows you to ...

随机推荐

  1. Milk(杭电1070)

    Milk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  2. XML系统学习

    参考:W3School XML基本概念 1.XML是eXtensible Markup Language,使用DTD(Document Type Definition)来描述数据,主要是为传输和存储数 ...

  3. PHP 7给我震撼

    看了一些php7(ng)的讨论,目前还没有去下beta版尝试编译. 作为一个phper,一直都有关注php本身语言的发展.以前在jumei工作的时候就听罗sir谈到过php ng,性能将超过faceb ...

  4. hadoop无法启动DataNode问题

    因为种种原因,今天又一次安装hadoop集群.清空了/tmp下的文件夹,重新启动集群,hadoop namenode -format 之后  start-all   可是没有发现DataNode的守护 ...

  5. UESTC--1263--The Desire of Asuna(贪心)

    The Desire of Asuna Time Limit: 1000MS   Memory Limit: 65535KB   64bit IO Format: %lld & %llu Su ...

  6. Oracle RAC 全局等待事件 gc current block busy 和 gc cr multi block request 说明--转载(http://blog.csdn.net/tianlesoftware/article/details/7777511)

    一.RAC 全局等待事件说明 在RAC环境中,和全局调整缓存相关的最常见的等待事件是global cache cr request,global cache busy和equeue. 当一个进程访问需 ...

  7. linux系统oracle服务自启动

    终于知道为什么自启动脚本一直无法成功执行,原来都是空格不对惹的祸.具体步骤说明如下: 1.修改dbstart和dbshut脚本 dbstart脚本默认值启动oracle服务,不启动监听服务,如果想在启 ...

  8. 11. Container With Most Water[M]盛最多水的容器

    题目 Given \(n\) non-negative integers \(a_1,a_2,\cdots,a_n\), where each represents a point at coordi ...

  9. HttpClient简单操作

    HttpClient 这个框架主要用来请求第三方服务器,然后获取到网页,得到我们需要的数据: HttpClient设置请求头消息User-Agent模拟浏览器 比如我们请求 www.tuicool.c ...

  10. java实现sql批量插入参数

    背景: 需要更新一些不规范的时间格式,如将某个时间格式化为yy-MM-dd,实际上为 yy-MM-dd hh:mm:ss,并且需要提供回滚脚本. 例如:规范化时间的脚本如下: ,) WHERE tes ...