I've finally got around to looking in to a little problem that's been bugging me for a while, and has recently become a real problem for a web application I maintain. When a user clicks on a link to download their document (in this case a PDF file, but it could be in any format) they get the option to save it, but the file selector suggests the filename image.cgi, which isn't exactly appropriate. We don't want (potentially non-technical) users to have to rename the file so that it opens in Acroread or whatever when they double click on it.
Suggesting a filename
There is a way to fix this, I found. You can add a Content-Disposition
header to your HTTP response. This header was originally designed for
email MIME attachments, to indicate whether they should be treated like an
attachment or displayed ‘inline’ with the main content. The
header also allows for an optional filename= parameter, and
web browsers seem to pay attention to it.
Here's the HTTP headers I'm using:
Content-Type: application/pdf Content-Disposition: inline; filename=document.pdf
Exactly how you put this in to the HTTP response will obviously vary depending on what programming language you're using. For my CGI scripts I can just print it out.
I'm not sure which of inline or attachment
would make more sense here, but inline seems to work fine
for me, so I'll leave it at that.
One other thing: the filename you use shouldn't have a path (so no forward slashes or backslashes). When I accidently had a forward slash in the name it made IE go crazy.
Definition of Content-Disposition
Annoyingly, the Content-Disposition header isn't actually defined in the HTTP/1.1 standard (RFC 2616). It is defined separately in RFC 1806 though, and it seems to be widely implemented.
I've checked this and it works in IE 5, IE 5.5, IE 6, Firefox 0.8, and some version of Lynx.