Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: How can I get the browser to open and display a PDF instead of displaying a download prompt?

Ok, so I have an action method that generates a PDF and returns it to the browser. The problem is that instead of automatically opening the PDF, IE displays a download prompt even though it knows what kind of file it is. Chrome does the same thing. In both browsers if I click a link to a PDF file that is stored on a server it will open up just fine and never display a download prompt.

Here is the code that is called to return the PDF:

public FileResult Report(int id) {     var customer = customersRepository.GetCustomer(id);     if (customer != null)     {         return File(RenderPDF(this.ControllerContext, "~/Views/Forms/Report.aspx", customer), "application/pdf", "Report - Customer # " + id.ToString() + ".pdf");     }     return null; } 

Here's the response header from the server:

HTTP/1.1 200 OK Server: ASP.NET Development Server/10.0.0.0 Date: Thu, 16 Sep 2010 06:14:13 GMT X-AspNet-Version: 4.0.30319 X-AspNetMvc-Version: 2.0 Content-Disposition: attachment; filename="Report - Customer # 60.pdf" Cache-Control: private, s-maxage=0 Content-Type: application/pdf Content-Length: 79244 Connection: Close 

Do I have to add something special to the response to get the browser to open the PDF automatically?

Any help is greatly appreciated! Thanks!

like image 525
Jeff Camera Avatar asked Sep 16 '10 06:09

Jeff Camera


People also ask

How do I get a PDF to open in a new tab instead of downloading it?

At the top right, click More Settings. At the bottom, click Show advanced settings. Under “Privacy”, click Content settings. Under “PDF Documents," check the box next to "Open PDF files in the default PDF viewer application.” (Uncheck this box if you want PDFs to open automatically when you click them.)


2 Answers

Response.AppendHeader("Content-Disposition", "inline; filename=foo.pdf"); return File(... 
like image 107
Darin Dimitrov Avatar answered Sep 22 '22 13:09

Darin Dimitrov


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.

like image 26
Marnix van Valen Avatar answered Sep 23 '22 13:09

Marnix van Valen