Apache:下载在每个 LF 之前插入 CR 的文件(甚至是电子表格)

发布于 2024-08-16 23:56:16 字数 779 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奶气 2024-08-23 23:56:16

使用 msvcrt.setmode(1, os.O_BINARY) (在编写标头和 sys.stdout.flush 后)将标准输出设置为二进制模式 - 可怜的 Apache 是无辜的,它是 Windows 的东西;-)。

Use msvcrt.setmode(1, os.O_BINARY) (after you write the headers and sys.stdout.flush them) to set standard output to binary mode -- poor Apache is innocent, it's a Windows thing;-).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文