HTTP Headers to make a download

M

Michael Foord

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


TIA

Fuzzy

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

Michael Fuhr

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.
 
M

Michael Foord

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top