ASP.NET MVC: How can I get the browser to open and display a PDF instead of displaying a download prompt?
Response.AppendHeader("Content-Disposition", "inline; filename=foo.pdf");
return File(...
On the HTTP level your 'Content-Disposition' header should have 'inline' not 'attachment'. Unfortunately, that's not supported by the FileResult (or it's derived classes) directly.
If you're already generating the document in a page or handler you could simply redirect the browser there. If that's not what you want you could subclass the FileResult and add support for streaming documents inline.
public class CustomFileResult : FileContentResult
{
public CustomFileResult( byte[] fileContents, string contentType ) : base( fileContents, contentType )
{
}
public bool Inline { get; set; }
public override void ExecuteResult( ControllerContext context )
{
if( context == null )
{
throw new ArgumentNullException( "context" );
}
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = ContentType;
if( !string.IsNullOrEmpty( FileDownloadName ) )
{
string str = new ContentDisposition { FileName = this.FileDownloadName, Inline = Inline }.ToString();
context.HttpContext.Response.AddHeader( "Content-Disposition", str );
}
WriteFile( response );
}
}
A simpler solution is not to specify the filename on the Controller.File
method. This way you will not get the ContentDisposition header, which means you loose the file name hint when saving the PDF.