我是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文件时,它没有被打开。任何人都帮助我。感谢
答案 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())