20

I am trying to get a file (array buffer) from my backend using the new HttpClient.

My code is pretty much like the following:

this.http.post(url, body, {responseType: 'blob', observe: 'response'})
  .subscribe(resp => {
    console.log(resp.headers.get('content-disposition'));
  });

The repsonse in my browser console is null

When I take a look at the network tab, then I see that content-disposition has a value like attachment; filename="whatever"

If I try to get content-type instead, then I receive something.

Have you experienced something similar or can you tell me what I have to do else? Many thanks

Tobias Etter
  • 744
  • 1
  • 6
  • 19
  • there is a relation to https://stackoverflow.com/questions/46389488/angular-4-3-httpclient-empty-response-headers but I could not solve it yet – Tobias Etter Nov 07 '17 at 15:00

2 Answers2

57

I had the same Issue and found this https://stackoverflow.com/a/45482346/1909698

It worked for me after adding 'Access-Control-Expose-Headers': 'Content-Disposition' in my backend response header.

kair
  • 946
  • 1
  • 10
  • 16
  • That was exactly what I searched for. Solved my problem. Thank you =) – Tobias Etter Nov 08 '17 at 09:08
  • Thanks! This was also useful: https://stackoverflow.com/questions/38897764/asp-net-core-content-disposition-attachment-inline – Web Dev Sep 06 '18 at 01:43
  • Thanks great find, the issue is so misleading because in browser you see the content-disposition but angular does not see it until you tell the server specifically to expose the specific header. – MG Developer Feb 11 '19 at 01:33
  • I didn't need 'Access-Control-Expose-Headers': 'Content-Disposition' in Angular 12 (anymore?!) – Youp Bernoulli Aug 20 '21 at 13:28
0

if your backend is using SpringSecurity you need add ExposeHeader to configuration source:

@Bean
protected CorsConfigurationSource corsConfigurationSource() {
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.addExposedHeader("Content-Disposition");
    return source;
}
Alessio
  • 3,404
  • 19
  • 35
  • 48
Xy718
  • 1
  • 1