使用StaticFileHandler在Tornado Python上托管文件 [英] Using StaticFileHandler to host a file on Tornado Python

查看:298
本文介绍了使用StaticFileHandler在Tornado Python上托管文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Tornado中使用StaticFileHandler,并且大部分情况下都可以使用它,除了单击下载时在网页中输出文件(.csv)之外.我保存文件的唯一方法是右键单击并说将目标另存为"(但这并非在所有浏览器中都有效).

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

如何强制文件下载? 我知道我需要以某种方式设置StaticFileHandler的标头:

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.

感谢您的时间!

推荐答案

扩展web.StaticFileHandler

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!

这篇关于使用StaticFileHandler在Tornado Python上托管文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆