Apache:下载在每个 LF 之前插入 CR 的文件(甚至是电子表格)
我在 Windows XP 上使用 Apache 2.2.14 和 Python 2.6 进行 CGI。通过 CGI 发送的文件被损坏。在每个 LF 之前插入一个 CR。 Firefox、IE 和 Curl 客户端给出相同的结果。文件大小正确,但在整个文件中插入了 CR,并且数据向下移动并被截断。我可以查看服务器上的文件,没有问题。
我缺少 Apache 中的某些开关吗?
以下是编写 HTTP 标头并发送文件的 python 代码:
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())
标题如下所示:
Content-type: application/octet-stream Content-Length: 84210 Content-Disposition: attachment; filename="product.xls"
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
msvcrt.setmode(1, os.O_BINARY)
(在编写标头和 sys.stdout.flush 后)将标准输出设置为二进制模式 - 可怜的 Apache 是无辜的,它是 Windows 的东西;-)。Use
msvcrt.setmode(1, os.O_BINARY)
(after you write the headers andsys.stdout.flush
them) to set standard output to binary mode -- poor Apache is innocent, it's a Windows thing;-).