473,511 Members | 15,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HTTP Headers to make a download

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/atlantib...thonutils.html
Jul 18 '05 #1
2 3139
fu******@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/
Jul 18 '05 #2
mf***@fuhr.org (Michael Fuhr) wrote in message news:<41**********@omega.dimensional.com>...
fu******@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/atlantib...thonutils.html
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5092
by: rick | last post by:
Hi everyone... I am looking to write code that can read a file from an http address (for example: http://www.somesite.com/document.xml) and check it for any changes (an updated state) then...
10
3026
by: Lisa Pearlson | last post by:
Hi, I have a php script with no more than this: <?php echo "Hello World!"; ?> When a webbrowser client requests data, it receives Apache server headers, followed by my data: HTTP/1.1 200...
7
9263
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
16
1466
by: Grumble | last post by:
Hello, I recently came across code that defined a peculiar macro: #define local static gcc had no problem with it, but another (experimental) compiler I am evaluating output a lengthy list...
1
1482
by: Abhishek Srivastava | last post by:
Hello All, Apologies to everyone, I posted this before but received no response. I feel someone must have come accross this problem before, and I need a solution desparately. I have written a...
1
4958
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType =...
2
1787
by: jethro_uk | last post by:
Hi Guys, I have a problem, and I would be grateful for any help. My WebApp has to do some database work in page1.ASP Because this may take a few second, before Page1 connects to the database,...
3
1613
by: mpar612 | last post by:
I am trying to create an area where a user can come to a page that queries a database and displays links to files. Here is the code for the link: <td><a...
2
3905
by: silk.odyssey | last post by:
I am getting the following error trying to download an html page using urllib2. urllib2.HTTPError: HTTP Error 204: NoContent The url is of this type: ...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.