HTTP Headers to make a download

Michael Foord fuzzyman at gmail.com
Wed Oct 13 12:56:22 CEST 2004


mfuhr at fuhr.org (Michael Fuhr) wrote in message news:<416ca56c$1_3 at omega.dimensional.com>...
> 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.

My test was with IE 6 (I think ? - the default XP one anyway) and
sending a zipfile - so binary data.

Hmmm.......

'Save Target As' ought to work... but I'd rather be able to let the
browser know it's expecting a download.
(I ought to test that 'save target' *does* work properly...)

Regards,


Fuzzy

http://www.voidspace.org.uk/atlantibots/pythonutils.html



More information about the Python-list mailing list