Javascript Tricks, Code Samples and Snippets
/ JTricks.com Home / Using Content-Disposition header - forcing SaveAs in browsers
Last updated: 11 Feb 2006
Using Content-Disposition header - forcing SaveAs in browsers
There are situations (to save a documentation in pdf format or a financial document) where you might want a hyperlink leading to a file to present a SaveAs dialog in browser. Ways to do that exist. However, flaky browser support makes its usage a walk in the mine field.
Using javascript
By means of javascript you can provide a dialog to save the page that is currently displayed (if the user is looking through Internet Explorer at your page that is).

For example:  Save this page

This is done through the following code:

<a href="javascript:void(0);"
 onclick="document.execCommand('SaveAs',true,'file.html');"
 >Save this page</a>


However, usually you want to save another file, the file a hyperlink leads to. To do that javascript is not enough (at least there is no such standard way) and something must be done on the server.
Using the HTTP header
In order to force the browser to show SaveAs dialog when clicking a hyperlink you have to include the following header in HTTP response of the file to be downloaded:

Content-Disposition: attachment; filename=<file name.ext>

Where <file name.ext> is the filename you want to appear in SaveAs dialog (like finances.xls or mortgage.pdf) - without < and > symbols.

You have to keep the following in mind:

  • The filename should be in US-ASCII charset.
  • The filename should not have any directory path information specified.
  • The filename should not be enclosed in double quotes even though most browsers will support it.
  • Content-Type header should be before Content-Disposition.
  • Content-Type header should refer to an unknown MIME type (at least until the older browsers go away).
Browser details - Internet Explorer
It seems that Internet Explorer has the worst track record of the Content-Disposition header support. The feature had many security vulnerabilities and as a result Microsoft changed the code many times sometimes breaking it altogether.

Practice has shown that download code uses a different HTTP header parser. And if that parser is unsuccessfull at handling all header lines, IE will fail to download file at all.

Version specifics:

Internet Explorer 6.0 Mostly ok.

As with Internet Explorer 5.01 there are problems in 6.0 Service pack 1 with handling Content-Disposition headers more than 150 bytes long.

Microsoft Knowledgebase articles: 816868
Internet Explorer 5.5 5.5 will try to show the file in browser window instead of downloading.

This problem was fixed in 5.5 Service Pack 1, but another one was introduced instead - it may save the referring document instead of the downloaded file.

This problem was fixed in 5.5 service pack 2.

Microsoft Knowledgebase articles: 267991 279667 281119
Internet Explorer 5.0 If the filename extension is not known, it will add it to the filename twice when saving the file.

For example, "newName.zzz" would be saved as "newName.zzz..zzz".

The bug was currected in 5.01.

Another problem was inability to handle Content-Disposition headers more than 150 bytes long. It was fixed in 5.01 Service pack 3.

Microsoft Knowledgebase articles: 262042 816868
Internet Explorer 4.01 Doesn't work.

Miscrosoft started supporting Content-Disposition:attachment header with Internet Explorer 5.0.

It might read the filename information from the header though if it was going to present the SaveAs dialog due to unrecognized file MIME type (if MIME sniffing fails).

Microsoft Knowledgebase articles: 182315
Browser details - Mozilla/Firefox
Version specifics:

Firefox 1.5 Will handle the header.
Firefox 1.0.x Even though there were several security vulnerabilities, Firefox seems to have adequate support for the Content-Disposition header.
Netscape 4.x Will not handle Content-Disposition. However, it will present Save As dialog if the it doesn't recognize Content-Type. Therefore setting Content-Type to nonexistent type will help.
Browser details - Opera
Opera supports the Content-Disposition header since at least version 6. Opera also have had security problems related to the header, but as in case with Firefox it works.
Try it!
Try the header support in your browser, click here:

© JTricks.com 2005-2009. All rights reserved.