HTTP Headers to make a download

Michael Fuhr mfuhr at fuhr.org
Wed Oct 13 05:47:56 CEST 2004


fuzzyman at gmail.com (Michael Foord) writes:

> I'm creating a CGI that offers files for download. After googling I
> though I had the right headers to send to force a download - but it
> sends the data to the browser instead of opening the download 'save
> file' dialog. Can anyone see what I'm doing wrong/suggest an
> alternative ?
>
> responseline = '''Content-Type: application/octet-stream
> Content-Disposition: attachment; filename=%s
> Content-Length: %s
> '''
>
> print responseline % (filename, len(filedata))
> print filedata

What browser and platform are you using?  Not all browsers honor
Content-Disposition -- some base their display-or-download decision
on the content itself.  For example, using Opera 7.54 on FreeBSD
and your example code, this string displays in the browser:

filedata = 'What hath Guido wrought?'

Whereas this string, different only in the first character, caused
the browser to present a download popup:

filedata = '\xbfWhat hath Guido wrought?'

In the first case, where the browser displayed the data, clicking
"Save" or "File->Save As" did use the file name that was in the
Content-Disposition header.

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/



More information about the Python-list mailing list