更改龙卷风网站中特定静态文件的mime类型

mik*_*ike 5 python tornado

我在/ static /目录服务器端有一堆文件,名称如下:

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  
Run Code Online (Sandbox Code Playgroud)

我在相同的域中获取它们,并定期将它们插入到iframe中,它们所做的只是渲染一些图像,但浏览器给出以下错误:

Resource interpreted as Image but transferred with MIME type text/html: "http://localhost:8888/static/Slide66.html". 
Run Code Online (Sandbox Code Playgroud)

我试图徒然地将staticfilehandler子类化:

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" )
Run Code Online (Sandbox Code Playgroud)

但是错误仍然存​​在,我该如何调整?

Ben*_*ell 6

在 Tornado 3.1 中,您可以子类化StaticFileHandler和覆盖get_content_type().

class StaticJSONFileHandler(tornado.web.StaticFileHandler):
    def get_content_type(self):
        return 'application/json'
Run Code Online (Sandbox Code Playgroud)