[Angular] Provide Feedback to Progress Events with Angular ' s HttpRequest Object

Source: Internet
Author: User
Tags export class

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. 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); }}

//ComponentImport {HttpClient, HttpRequest, Httpevent, httpeventtype} from ' @angular/common/http '; Uploadavatar (fileUpload) {const FormData=NewFormData (); Formdata.append (' Avatar ', fileupload.files[0], ' avatar.jpg ');  This. Peopleservice. Uploadavatar (formData). Subscribe (Res= {        if(Res.type = = = httpeventtype.uploadprogress) {const percentage= Math.Round (* res.loaded/res.total);  This. Output = ' File is ${percentage}%uploaded '; } Else if(Resinstanceof HttpResponse) {           This. Output =' File is completely uploaded ';  }      }); }

[Angular] Provide Feedback to Progress Events with Angular ' s HttpRequest Object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.