2

Using Blazor I am making an API call which returns an Excel file, I am trying to access the content-disposition response header which I can see is present using Chrome dev tools :

content-disposition: attachment; filename=report-285-20200603183055.xlsx; filename*=UTF-8''report-285-20200603183055.xlsx

I am using this code but it returns false :

if (httpResponseMessage.Headers.TryGetValues("content-disposition", out IEnumerable<string> extractedHeaders))

How can I access the content-disposition value, I want to extract the filename out? Just to add, I am exposing the header in Startup.cs ConfigureServices() in the API project :

            services.AddCors(o => o.AddPolicy("CorsPolicy", builder => {
            builder
            ...
            .WithExposedHeaders(new string[] { "Location", "location", "Content-disposition","content-disposition" })    
Paul
  • 856
  • 1
  • 8
  • 18

2 Answers2

1

You have to use

Response.Content.Headers

To get that header, Response.Headers does not contain it

reven
  • 330
  • 4
  • 14
1

You have to make sure that your API CORS policy exposes the header. See this question: Blazor WASM C# .NET 6 Content-Disposition is not Accessible

Blue Eyed Behemoth
  • 3,692
  • 1
  • 17
  • 27