Usually, when I want to transfer the text file of the web server to the client, this is what I did
import cgi print "Content-Type: text/plain" print "Content-Disposition: attachment; filename=TEST.txt" print filename = "C:\\TEST.TXT" f = open(filename, 'r') for line in f: print line
The ANSI file works very well. However, let's say I have a binary file a.exe (this file is in the secret path of the web server and the user will not have direct access to this directory path). I want to use a similar method for transmission. How can i do this?
- What content type should I use?
- Using print seems to have corrupted content received on the client side. What is the correct method?
I am using the following code.
#!c:/Python27/python.exe -u import cgi print "Content-Type: application/octet-stream" print "Content-Disposition: attachment; filename=jstock.exe" print filename = "C:\\jstock.exe" f = open(filename, 'rb') for line in f: print line
However, when I compare the downloaded file with the original file, it seems that there are additional spaces (or more) for each individual line.

source share