How to handle MIME type in tornado?

python, tornado

Solution

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

Problem

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

Original source