Twisted web problem when serving docx files

59 views Asked by At

I have a question for you!

I'm running a simple webserver with twistd web and it works great must of the time. I have a problem serving .docx files.

Let me explain with an example. On my webserver I have two files: file.pdf and file.docx (the x is important).

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). 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.

It is not a browser issue, because if a click on a docx file served from another webserver, the browser will download it.

I'm starting the webserver directly from the windows cmd prompt using twistd. The line looks like this:

twistd -no web --path d:\shares\

The question is: how can I tell twistd to force the download of docx file the same way it does for pdf?

Thanks

1

There are 1 answers

3
Josh Friedlander On

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. 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.