Using HttpHandler with Content-Disposition of inline loses filename when saving PDF RRS feed

  • Question

  • User768014394 posted

    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";

    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?

    Tuesday, June 5, 2012 7:23 PM


  • User-821857111 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 6, 2012 3:20 AM