Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle MIME type in tornado?

Tags:

python

tornado

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

like image 991
dhana Avatar asked Aug 21 '13 10:08

dhana


People also ask

What is a MIME type used for?

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.

What is Tornado web application?

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.


1 Answers

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())
like image 186
alecxe Avatar answered Sep 28 '22 06:09

alecxe