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. 支付宝接口程序、文档及解读(ASP.NET)

    最近需要为网站加入支付宝的充值接口,而目前关于支付宝接口开发的资料比较杂乱,这里就我此次开发所用到的资料进行汇总整理,希望能够帮助需要的朋友. 开发步骤: 1. 确定签约类型 支付宝的接口有多种类型, ...

  2. UVA - 11722 Joining with Friend 几何概率

                            Joining with Friend You are going from Dhaka to Chittagong by train and you ...

  3. excel如何将一列按奇偶数分成两列

    借助于函数.上图说明一切: 方法一.OFFSET函数, 奇数列公式:C1=OFFSET($A$1,ROW()*2-2,), 偶数列公式:D1=OFFSET($A$1,ROW()*2-1,) 一起下拉即 ...

  4. Java-MyBatis:MyBatis XML 文件

    ylbtech-Java-MyBatis:MyBatis XML 文件 1.返回顶部 1. Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大, ...

  5. C# First and FirstOrDefault 方法详解

    在工作中我们经常会遇到有关LINQ 的一些问题.这时我们就用到lambda 表达式. 下面是我在工作遇到的. First  and FirstOrDefault  这两方法.我今天把它记录一下. 需要 ...

  6. 20.QT文本文件读写

    #include "mainwindow.h" #include "ui_mainwindow.h" #include <QFile> #inclu ...

  7. Session会在浏览器关闭后消失吗?

    转  http://blog.csdn.net/rongwenbin/article/details/51784310 Cookie的两种类型   在项目开发中我们时常将需要在客户端(浏览器)缓存的数 ...

  8. docker初安装的血泪史

    最近docker很火,不管是朋友圈内还是公司内聊天都离不开docker,于是对docker产生了极大的好奇心,凭着一颗程序猿的好奇心开始了docker的安装血泪史. 首先我有一台从公司退役的本本x22 ...

  9. ubuntu DNS 出错,用以下命令可以解决

    具体的错误为:DNS_PROBE_FINISHED_BAD_CONFIG 命令为: sudo rm /etc/resolv.conf sudo ln -s ../run/resolvconf/reso ...

  10. pyCrypto python 3.5--转

    原文地址:https://gxnotes.com/article/198426.html 问题描述 我发现一些PyCrypto安装程序为Python 3.3和3.4,但没有任何Python 3.5. ...