I want to serve a static SVG file with Flask, but the SVG file is transferred without a Content-Type
header. The correct mime type is image/svg+xml
. How can I make sure that Flask uses the correct mime type for the SVG file and sends it to the browser?
我想用Flask提供一个静态SVG文件,但是SVG文件是在没有内容类型头的情况下传输的。正确的mime类型是图像/svg+xml。如何确保Flask使用了SVG文件的正确mime类型并将其发送到浏览器?
I refer to the file as /static/python.svg
, and it does exist.
我将该文件称为/static/python。svg,它确实存在。
I tried this in my __init__.py
file, but it didn't make any difference:
我在我的房间里试过这个。py文件,但是没有任何区别:
import mimetypes
mimetypes.add_type('images/svg+xml', '.svg')
2 个解决方案
#1
7
There is a mistake in your mime type. The correct one is image/svg+xml
(note the lack of the ‘s’).
您的mime类型中有一个错误。正确的是图像/svg+xml(注意缺少“s”)。
import mimetypes
mimetypes.add_type('image/svg+xml', '.svg')
# ^ no s
#2
1
A easy (but hacky) way is to add a new route just for svgs:
一种简单(但不容易)的方法是为svgs添加一条新路线:
@app.route('/static/<svgFile>.svg')
def serve_content(svgFile):
return file('static/'+svgFile+'.svg').read()
#1
7
There is a mistake in your mime type. The correct one is image/svg+xml
(note the lack of the ‘s’).
您的mime类型中有一个错误。正确的是图像/svg+xml(注意缺少“s”)。
import mimetypes
mimetypes.add_type('image/svg+xml', '.svg')
# ^ no s
#2
1
A easy (but hacky) way is to add a new route just for svgs:
一种简单(但不容易)的方法是为svgs添加一条新路线:
@app.route('/static/<svgFile>.svg')
def serve_content(svgFile):
return file('static/'+svgFile+'.svg').read()