Content-Disposition inline filename issue with IE

Ragavan picture Ragavan · Mar 23, 2016 · Viewed 11.1k times · Source

I am displaying a pdf in browser with inline from API using an aspx page.

While saving the pdf using Chrome/Firefox, takes the filename from
header("Content-Disposition", "inline;filename=xyz.pdf")

But while saving the pdf using IE it does not reads the filename from
header("Content-Disposition", "inline;filename=xyz.pdf").
instead it takes the aspx name.

Technical details

I have an xyz.aspx page. The xyz.aspx page will invoke an API for a document. Then the downloaded document from API will transferred to browser with inline to display the pdf document. Am setting the response header as below and writing the file bytes.

            HttpContext.Current.Response.ClearHeaders();

            Response.AddHeader("Content-Disposition", "inline;filename=\"" + Name + "\"");

            HttpContext.Current.Response.ContentType = "application/pdf";
  • Issue:

    While saving the opened pdf in IE it takes xyz.aspx instead of the name from header.

  • Requirement:

    While saving the pdf using IE, it need to save with the name of pdf.

I googled so much, as every one tells its IE behavior. I hope some one knows a solution.

Note: I have to display the pdf in browser and then save. Not to download using "attachment"

Answer

Hikariii picture Hikariii · Mar 23, 2016

It is true some versions of IE can't handle ("Content-Disposition", "inline;filename=...")

This is because filename=... was originally intended for the attachment disposition. Not all browser-based PDF viewers can handle it.

The only solution I see is to allow access via a different url. Suppose you have a route to the pdf like: /pdf/view. If you change it to /pdf/view/filename and you configure your application to handle this route in the same way as /pdf/view your problem is solved.

You can also re-write the download url on the webserver. Depending on your webserver you have various ways of doing this.