Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read content-disposition headers from server response angular 2

I am unable to find, how to read filename from content disposition in angular 2 documentation. Can someone guide read the headers from server in angular 2content headers

like image 570
Sahil Agarwal Avatar asked Mar 20 '17 07:03

Sahil Agarwal


1 Answers

With new Angular Http, one can try the following in the Service code.

  downloadLink(): Observable<HttpResponse<Blob>> {     return this.http.get<Blob>(this.someURL, {       observe: 'response',       responseType: 'blob' as 'json'     });   } 

And use the above as

 this.someService   .downloadLink()   .subscribe(     (resp: HttpResponse<Blob>) => {       console.log(resp.headers.get('content-disposition'));       data = resp.body     }); 

Also, on the server side, one needs to set the following header in response. 'Access-Control-Expose-Headers': 'Content-Disposition'

Like in Java Spring Boot one can do the same using

    final HttpHeaders headers = new HttpHeaders();     headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=1.xlsx");     headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION); 
like image 180
Nehal Damania Avatar answered Oct 05 '22 04:10

Nehal Damania