yswanderer
yswanderer

Reputation: 212

Response header Content-Disposition not doing anything

I am trying to retrieve a file from my server (which is using ASP.NET Core MVC) and display it in the browser. After looking through many threads on here and other sites, the same solution seems to be suggested every time; which is to use Content-Disposition inline in the response header. I have my code as follows:

IFileInfo file = _fileProvider.GetFileInfo(filename);
Response.Headers.Add("Content-Disposition", $"inline; {file.Name}");
return File(file.CreateReadStream(), "application/pdf", file.Name);

While this seems to be the correct code based on other threads I've seen, the Content-Disposition header does absolutely nothing in my code. Whether I have the header, remove it, or make it something else (i.e. attachment), it downloads and saves the file but does not display it unless I manually open the file.

Does anyone know what I'm missing here?

Upvotes: 1

Views: 4820

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239440

Returning the File overload that accepts the filename param is overwriting your Content-Disposition header to be attachment; {filename}. Just do:

return File(file.CreateReadStream(), "application/pdf");

And you'll be fine.

Upvotes: 8

Related Questions