如何在 Django 中为 Chromium 正确设置 mime-type

How to correctly set mime-type for Chromium in Django

在 Django 中,我即时提供一个 png 图像:

from matplotlib.pyplot import figure
fig_ATF = figure(figsize=(5, 5), facecolor='white')
....
response = HttpResponse(content_type='image/png')
fig_ATF.savefig(response, format='png')
fig_ATF.clear()
response['Content-Length'] = len(response.content)
return response

Firefox 正确地将 Content-Type 解释为 image/png。然而,Chromium 不显示图像并抱怨:Resource interpreted as Image but transferred with MIME type text/html

这些是 Chromium 中的相关响应 headers:

Cache-Control:max-age=0
Connection:close
Content-Encoding:gzip
Content-Length:20
Content-Type:text/html; charset=utf-8
Date:Tue, 17 Mar 2015 10:08:36 GMT
Expires:Tue, 17 Mar 2015 10:08:37 GMT
Last-Modified:Tue, 17 Mar 2015 10:08:37 GMT
Server:Apache
Vary:Cookie,Accept-Encoding
X-Frame-Options:SAMEORIGIN

编辑 直接访问图像时,图像在 Chromium 中加载得很好。这个问题似乎与它通过jQuery添加ajax有关:

$('#img_location').html('<img src="/path/to/image.png?' + Date.now() + '" width="500" height="500" />');

通过在 Apache 的 httpd.conf 中添加以下行解决了该问题:

AddType image/png .png