您好,我正在尝试在 Tornado 中使用 StaticFileHandler,并且在大多数情况下它都可以正常工作,但当我单击下载时它会在网页中输出文件 (.csv)。我可以保存文件的唯一方法是右键单击并说将目标另存为(但这不适用于所有浏览器)。
如何强制下载文件? 我知道我需要像这样设置 StaticFileHandler 的 header :
self.set_header('Content-Type','text-csv')
self.set_header('Content-Disposition','attachment')
但我不知道如何设置它,因为它是默认处理程序。
感谢您的宝贵时间!
最佳答案
扩展 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)
不要忘记在处理程序中使用扩展类!
关于python - 使用 StaticFileHandler 在 Tornado Python 上托管文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17725025/