view raw
mike mike - 6 months ago 38
Python Question

change the mime-type of particular static files in tornado web

i have a bunch of files in my /static/ dir serverside with names like:

Slide0.html Slide121.html Slide143.html Slide165.html Slide187.html Slide208.html
Slide28.html Slide4.html Slide71.html Slide93.html
Slide100.html Slide122.html Slide144.html Slide166.html Slide188.html Slide209.html

and i fetch them on the same domain and insert them to a iframe periodically, all they do is to render some images, but the browser is giving the following error:

Resource interpreted as Image but transferred with MIME type text/html: "http://localhost:8888/static/Slide66.html".

i tried to subclass the staticfilehandler in vain:

class StaticHandler(tornado.web.StaticFileHandler):

def get(self, path):
abspath = os.path.abspath(os.path.join(self.root, path))
mime_type, encoding = mimetypes.guess_type(abspath)
if mime_type:
self.set_header("Content-Type", mime_type)

if 'Slide' in abspath:
self.set_header('Content-Type',"image/jpg" )

but the error persists how do i adjust it?


In Tornado 3.1 you can subclass StaticFileHandler and override get_content_type().

class StaticJSONFileHandler(tornado.web.StaticFileHandler):
    def get_content_type(self):
        return 'application/json'