gpt4 book ai didi

python - 如何处理 Tornado 中的 MIME 类型?

转载 作者:太空狗 更新时间:2023-10-29 21:40:14 25 4
gpt4 key购买 nike

我是 Tornado 框架的新手。当我设置 header 类型 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 文件时,它没有打开。任何人帮助我。谢谢

最佳答案

试一试:

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

关于python - 如何处理 Tornado 中的 MIME 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18354389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com