ASP.NET Core Content-Disposition Attachment/Inline

ASP.Net Core Content-Disposition attachment/inline

With version 2.0.0 of AspNetCore and AspNetCore.Mvc, I found none of the previous answers to be acceptable. For me, simply ommitting the filename argument to File was enough to trigger an inline content disposition.

return File(fileStream, contentType, fileName); // attachment
return File(fileStream, contentType); // inline

Update

In .NET 6, set the Content-Disposition header to inline or attachment by adding it to the response header:

// inline
Response.Headers.Add("Content-Disposition", "inline");
return File(fileStream, contentType);

// attachment
Response.Headers.Add("Content-Disposition", "attachment;filename=some.txt");
return File(fileStream, contentType);

Response header Content-Disposition not doing anything

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.

Content-Disposition inline does not work in Chrome

How to fix this?

Firefox help user to open file with a dialog but Edge and Chrome doesn't.
But you could choose the "Always open files of this type" option as below to open files automatically.

Sample Image


Operation in Firefox, Edge and Chrome browser

Sample Image
How to Undo "always open files of this type"?

  • Chrome

Sample Image

  • Edge

Sample Image



Related Topics



Leave a reply



Submit