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.
Operation in Firefox, Edge and Chrome browser
How to Undo "always open files of this type"?
- Chrome
- Edge
Related Topics
Getting Current Directory in .Net Web Application
How Can a Wpf Usercontrol Inherit a Wpf Usercontrol
Creating Powerpoint Presentations Programmatically
An Expression Tree May Not Contain a Call or Invocation That Uses Optional Arguments
Set Database Timeout in Entity Framework
Why Re-Initiate the Dbcontext When Using the Entity Framework
Remove the Title Bar in Windows Forms
Entity Framework Core Using Multiple Dbcontexts
Casting Object to Int Throws Invalidcastexception in C#
Recommend a Library/API to Unzip File in C#
Cookie Confusion with Formsauthentication.Setauthcookie() Method
Visual Studio 2015 Is Extremely Slow
Asynchronously Sending Emails in C#
How to Mock Out a .Net Httpwebresponse
Binding Datagrid to Observablecollection<Dictionary>
Automated Property with Getter Only, Can Be Set, Why
Do I Need to Lock or Mark as Volatile When Accessing a Simple Boolean Flag in C#