Suggesting a filename for downloaded files

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.

< Undeleting files with Subversion | Maintaining cached data with triggers in PostgreSQL >

Miniblog

(nuggets of inanity)

Tuesday Apr 24th 2007, 16:54 »
Just took the annual web design survey that AListApart do. I don't realy consider myself to be a web designer, but I have been doing a lot of HTML and CSS lately.
Monday Apr 23rd 2007, 18:23 »
Strange, there appears to be a bare-knuckle boxing match going on in the field outside my flat. Wish they wouldn't make so much noise about it.
Thursday Mar 1st 2007, 18:47 »
“In its written form, Hebrew has no vowels, making it the ideal language for texting.”
—Said in jest on some Radio 4 programme just now.

Archive: 2007 · 2006 · 2005 · 2004
Feed