We use an HttpHandler to serve up PDFs and had been displaying the document in the browser since Adobe Reader is installed. The problem with this is that if I set the Content-Disposition to inline, the file name is ignored and when the user goes to save
it, the name of the handler is used instead. If I set the Content-Disposition to attachment, the file name is preserved.
Code in GetPdf.ashx:
public void ProcessRequest(HttpContext context)
{
string filePath = context.Server.MapPath("~/App_Data/TestDocument.pdf");
byte[] file = File.ReadAllBytes(filePath);
string fileName = Path.GetFileName(filePath);
HttpResponse response = context.Response;
// A content disposition of "attachment" will force a "Save or Open" dialog to appear when
// navigating directly to this URL, and "inline" will just show open the file in the default viewer
response.AppendHeader("Content-Disposition", string.Format("inline; filename={0}", fileName)); // Name will be "GetPdf.pdf"
//response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}", fileName)); // Name will be "TestDocument.pdf"
response.AppendHeader("Content-Length", file.LongLength.ToString());
response.ContentType = "application/pdf";
response.BinaryWrite(file);
}
Is there a way to use a Content-Disposition of inline and keep the file name intact when the user saves it or am I stuck with having to use attachment instead?
It's up to the browser to decide what (if anything) to do with the file name that has been presented to it in the response headers. This isn't something you can affect programmatically.
Member
96 Points
422 Posts
Using HttpHandler with Content-Disposition of inline loses filename when saving PDF
Jun 05, 2012 07:23 PM|desertfoxaz|LINK
We use an HttpHandler to serve up PDFs and had been displaying the document in the browser since Adobe Reader is installed. The problem with this is that if I set the Content-Disposition to inline, the file name is ignored and when the user goes to save it, the name of the handler is used instead. If I set the Content-Disposition to attachment, the file name is preserved.
Code in GetPdf.ashx:
Is there a way to use a Content-Disposition of inline and keep the file name intact when the user saves it or am I stuck with having to use attachment instead?
All-Star
194881 Points
28101 Posts
Moderator
Re: Using HttpHandler with Content-Disposition of inline loses filename when saving PDF
Jun 06, 2012 03:20 AM|Mikesdotnetting|LINK
It's up to the browser to decide what (if anything) to do with the file name that has been presented to it in the response headers. This isn't something you can affect programmatically.