7

我是 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())

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

4

1 回答 1

8

试试看:

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())
于 2013-08-21T10:27:59.967 回答