Using StaticFileHandler to Host a Tornado Python File

Hi, I am trying to use StaticFileHandler in Tornado and, for the most part, its work, except for the output of the file (.csv) on the web page when I click the download button. The only way to save the file is to right-click and specify saving the target as (but this does not work in all browsers).

How to make a file load? I know that I need to somehow set the StaticFileHandler header as follows:

self.set_header('Content-Type','text-csv') self.set_header('Content-Disposition','attachment') 

But I do not know how to install it, because it is the default handler.

Thank you for your time!

+2
python file tornado static
source share
1 answer

Extend web.StaticFileHandler

 class StaticFileHandler(web.StaticFileHandler): def get(self, path, include_body=True): if [some csv check]: # your code from above, or anything else custom you want to do self.set_header('Content-Type','text-csv') self.set_header('Content-Disposition','attachment') super(StaticFileHandler, self).get(path, include_body) 

Do not forget to use the extended class in the handler!

+3
source share

All Articles