I am using Apache 2.2.14 and Python 2.6 for CGI on Windows XP. Files sent through CGI get corrupted. A CR gets inserted before every LF. Firefox, IE, and Curl clients give the same result. The file is the correct size, but CR’s are inserted throughout, and the data is shifted down and truncated. I can look at the file on the server, and it’s fine.
Is there some switch in Apache I am missing?
Here is the python code to write the HTTP header and send the file:
outsize = os.path.getsize(outfile)
mheader = "Content-type: application/octet-stream\n"
mheader = mheader + "Content-Length: "+str(outsize) + "\n"
mheader = mheader + "Content-Disposition: attachment; filename=\"product.xls\"\n\n"
sys.stdout.write(mheader)
sys.stdout.write(file(outfile, "rb").read())
The header looks like this:
Content-type: application/octet-stream
Content-Length: 84210
Content-Disposition: attachment; filename=”product.xls”
Use
msvcrt.setmode(1, os.O_BINARY)(after you write the headers andsys.stdout.flushthem) to set standard output to binary mode — poor Apache is innocent, it’s a Windows thing;-).