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. UVA 10593 Kites DP

    The season of flying kites is well ahead. So what? Let us make an inventory for kites. We are givena ...

  2. Unable to access the IIS metabase

    https://stackoverflow.com/questions/12859891/error-unable-to-access-the-iis-metabase 解决方法1 On Window ...

  3. CZLayer的阴影

    CALayer有一个shadow属性 意思是阴影 shadowcolor  //颜色 shadowoffset  //偏移 shadowOpacity  //透明度 layer有一个方法    mas ...

  4. Ubuntu14.04下初步使用MongoDB

    不多说,直接上干货! Ubuntu14.04下Mongodb(在线安装方式|apt-get)安装部署步骤(图文详解)(博主推荐) shell命令模式 输入mongo进入shell命令模式,默认连接的数 ...

  5. Web Api和Asp.Net mvc post请求区别

    这是mvc的,mvc的post请求可以这样写参数,但是web api的不行.而且content_type:"application/json" 必须要写 下面这是web api的: ...

  6. sql server 随机生成布尔值

    ) AS BIT) 或者 )

  7. swift使用查阅资料备份3

    自主学习之RxSwift(二) -----flatMap https://blog.csdn.net/chelongfei/article/details/50995603 RxSwift 系列(九) ...

  8. TF基础4

    模型的存储与加载 TF的API提供了两种方式来存储和加载模型: 1.生成检查点文件,扩展名.ckpt,通过在tf.train.Saver()对象上调用Saver.save()生成.包含权重和其他在程序 ...

  9. sqlhelper 数据库帮助操作类

    数据库帮助类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  10. CF960F Pathwalks_权值线段树_LIS

    很不错的一道思维题. Code: #include<cstdio> #include<algorithm> #include<iostream> using nam ...