Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use reportProgress in HttpClient in Angular? [duplicate]

Tags:

I am downloading file using HTTP POST method. I want to call another method to show download progress to end user until file download complete. How to use reportProgress in HttpClient for this.

  downfile(file: any): Observable<any> {      return this.http.post(this.url , app, {       responseType: "blob", reportProgress: true, headers: new HttpHeaders(         { 'Content-Type': 'application/json' },       )     });   } 
like image 569
karthik thurairaja Avatar asked Feb 27 '19 01:02

karthik thurairaja


People also ask

What is the return type of get () method of HttpClient API?

Use the HttpClient.get() method to fetch data from a server. The asynchronous method sends an HTTP request, and returns an Observable that emits the requested data when the response is received.

What is the use of HttpClientModule in Angular?

The HttpClientModule is a service module provided by Angular that allows us to perform HTTP requests and easily manipulate those requests and their responses. It is called a service module because it only instantiates services and does not export any components, directives or pipes.

What is the return type of HttpClient get request?

get() method is an asynchronous method that performs an HTTP get request in Angular applications and returns an Observable. And that Observable emits the requested data when the response is received from the server.

What does HttpClient post return Angular?

HttpClient. post() method is an asynchronous method that performs an HTTP post request in Angular applications and returns an Observable. HttpClient. post() has a type parameter similar to the HttpClient. get() request, through which we can specify the expected type of the data from the server.


2 Answers

You need to use reportProgress: true to show some progress of any HTTP request. If you want to see all events, including the progress of transfers you need to use observe: 'events' option as well and return an Observable of type HttpEvent. Then you can catch all the events(DownloadProgress, Response..etc) in the component method. Find more details in Angular Official Documentation.

  downfile(file: any): Observable<HttpEvent<any>>{      return this.http.post(this.url , app, {       responseType: "blob", reportProgress: true, observe: "events", headers: new HttpHeaders(         { 'Content-Type': 'application/json' },       )     });   } 

Then in component you can catch all the events as below.

this.myService.downfile(file)     .subscribe(event => {          if (event.type === HttpEventType.DownloadProgress) {             console.log("download progress");         }         if (event.type === HttpEventType.Response) {             console.log("donwload completed");         } }); 

Find HttpEventTypes Here.

like image 141
Sudarshana Dayananda Avatar answered Oct 05 '22 23:10

Sudarshana Dayananda


you will have to add HttpClientModule in your AppModule

you first need to build a request object by creating an instance of HttpRequest class and using reportProgress option.

For more information, please refer: https://alligator.io/angular/httpclient-intro/

like image 25
Shilpa Soni Avatar answered Oct 06 '22 01:10

Shilpa Soni