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. 项目工程的包package与文件夹的关系

    项目工程的包package与文件夹的关系: 1. 包名与文件夹是分层关系,包名只是一个字符串而已,包名.对应的是层级的文件夹. 如,com.Immoc.Access包,只是一个字符串.但他对应的win ...

  2. iOS绘图系统UIKit与Core Graphics

    概述 iOS主要的绘图系统有UIKit,Core Graphics,Core Animation,Core Image,Open GL等,本片博文主要介绍UIKit与Core Graphics的绘图系 ...

  3. mysql点滴_02程序中运行sql语句报字符集问题解决

    程序中运行  "SELECT t.EVENT_TYPE_ID FROM RATABLE_EVENT_TYPE t WHERE t.NAME='帐期末费用转移事件'"  报错 错误码 ...

  4. 【Android 应用开发】Android 平台 HTTP网速測试 案例 API 分析

    作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/25996817 工信部规定的网速測试标准 : 除普通网页測速 ...

  5. JAVA设计模式之【原型模式】

    1.案例一 学生复制 package Prototype; /** * Created by Jim on 2016/10/1. */ public class Student implements ...

  6. Pocket英语语法---六、感官动词接不同的动词表示什么意思

    Pocket英语语法---六.感官动词接不同的动词表示什么意思 一.总结 一句话总结:其实进行时一般是表示连续,动词原形一般表示常态,过去分词一般表示被动(或者完成). 感官动词接原型表示动作的一般情 ...

  7. ROS-OPENCV

    前言:opencv是一个开源的跨平台计算机视觉库. 前提:1.已下载并编译了相关功能包集,如还未下载,可通过git下载:https://github.com/huchunxu/ros_explorin ...

  8. js 手机号码简单正则校验

    现在手机号码的号段有如下几种,包括17年新发出的三个(166,199,198)号段. 在一些项目注册登录或者其他中,涉及到手机号进行一个简单的有效验证,在前端先进行一个简单的检验: 判断字符串是否符合 ...

  9. Data type-数据类型

    操作方式.含义.存储方式. In computer science and computer programming, a data type or simply type is a classifi ...

  10. 1.Vector(同步)和ArrayList(异步)异同

    Vector线程同步,线程安全   ArrayList的运行速度较快,因为没有使用线程 请求方式:同步   异步未响应=假死 原因:1.占用内存过多,内存无法进行处理  2.并发量(本机的浏览器进行访 ...