简体   繁体   English

提供 docx 文件时出现扭曲的 web 问题

[英]Twisted web problem when serving docx files

I have a question for you!我有一个问题问你!

I'm running a simple webserver with twistd web and it works great must of the time.我正在运行一个带有扭曲 web的简单网络服务器,并且它必须在时间上很好地工作。 I have a problem serving .docx files.我在提供.docx文件时遇到问题。

Let me explain with an example.让我用一个例子来解释。 On my webserver I have two files: file.pdf and file.docx (the x is important).在我的网络服务器上,我有两个文件: file.pdffile.docx (x 很重要)。

Now, on my browser, if I enter the URL of the pdf file, the browser will start the download (or open it depending on user preferences).现在,在我的浏览器上,如果我输入 pdf 文件的 URL,浏览器将开始下载(或根据用户偏好打开它)。 This is the expected behavior.这是预期的行为。 But if I enter a link to a docx, instead of downloading it, the browser will display it as a sequence of strange letters and numbers.但是如果我输入一个 docx 的链接,而不是下载它,浏览器会将它显示为一系列奇怪的字母和数字。

It is not a browser issue, because if a click on a docx file served from another webserver, the browser will download it.这不是浏览器问题,因为如果单击另一个网络服务器提供的 docx 文件,浏览器会下载它。

The question is: how can I tell twistd to force the download of docx file the same way it does for pdf?问题是:我如何告诉twistd 强制下载docx 文件,就像它对pdf 一样?

Thanks谢谢

It might help if you shared some of your code, but I think the basic idea is that you should add the correct MIME type to the header that your server returns, which will help the browser know what to do with it rather than try to render it as text.如果您共享一些代码可能会有所帮助,但我认为基本思想是您应该向服务器返回的 header 添加正确的MIME 类型,这将帮助浏览器知道如何处理它而不是尝试渲染它作为文本。 Based on the docs here it looks like you want something like this:根据此处的文档,您似乎想要这样的东西:

from twisted.web import static

root = static.File("/files")
root.contentTypes[".docx"] = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

Using the somewhat long-winded MIME type for docx.对 docx 使用有点冗长的 MIME 类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM