0

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

toto
  • 110
  • 1
  • 6

1 Answers1

0

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.

Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75
  • thanks for your help, actually I cannot share any code because I'm just starting twistd (the demon tool) from the command line with just a few arguments. I add the command line to my original question. – toto Sep 15 '22 at 14:17
  • can I add this content type definition from the command line as well? – toto Sep 15 '22 at 14:20
  • OK, as far as I understand you need a plugin to do this and can't do it from the command line. But also I tried to reproduce your situation, and opening the link to a `docx` file correctly triggers a download, without any configuration. Opening the root page shows each file in the directory and its MIME type, and the `docx` shows the correct type. Maybe this is an issue on the browser or OS level? – Josh Friedlander Sep 15 '22 at 17:28