如何处理龙卷风中的MIME类型?

时间:2013-08-21 10:00:13

标签: python tornado

我是Tornado框架的新手。当我设置标题类型application/pdf时,它只采用默认的MIME类型,即; plian/text。这是我的代码,

class MainHandler(tornado.web.RequestHandler):
    def get(self):
            ifile = open("requirements.txt", "r")
            self.set_header('Content-Type', 'application/pdf; charset="utf-8"')
            self.set_header('Content-Disposition', 'attachment; filename="test.pdf"')
            #print(self.list_headers())
            self.write(ifile.read())

通过网络浏览器成功下载。这里url http:/203.193.173.102:8888 /。 但是当我打开pdf文件时,它没有被打开。任何人都帮助我。感谢

1 个答案:

答案 0 :(得分:7)

试一试:

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        with open('test.pdf', 'rb') as f:  
            self.set_header("Content-Type", 'application/pdf; charset="utf-8"')
            self.set_header("Content-Disposition", "attachment; filename=test.pdf")                  
            self.write(f.read())