Using StaticFileHandler to host a file on Tornado Python

独自空忆成欢 提交于 2019-12-24 01:18:37

问题


Hi I am attempting to use StaticFileHandler in Tornado and for the most part its working, except its outputting the file (.csv) in a webpage when I click download. The only way I can save the file is Right clicking and saying save target as (but this doesn't work in all browsers).

How can I force the file to be downloaded? I know I need to somehow set the header of the StaticFileHandler like this:

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

But I have no idea how to set it because it is a default handler.

Thanks for your time!


回答1:


Extend the 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)

Dont forget to use your extended class in the handler!



来源:https://stackoverflow.com/questions/17725025/using-staticfilehandler-to-host-a-file-on-tornado-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!