I am new to Tornado framework. When I set the header type application/pdf
, But it takes only default MIME Type i.e; plian/text
. Here my code,
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())
It is downloading successfully through web browser. Here url http:/203.193.173.102:8888/. But when I open the pdf file it is not opened. Any one help me. Thanks
MIME types—also sometimes called Internet media types or Content-types—describe the media type of content either contained in email or served by web servers or web applications, and are intended to help guide a web browser to correctly process and display the content.
A Tornado web application generally consists of one or more RequestHandler subclasses, an Application object which routes incoming requests to handlers, and a main() function to start the server.
Give it a try:
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With