flask+mongoengine做一小站,model中使用FileField字段类型存储上传的图片到文档,该文档记录其它文本字段已经以{{ xx.字段 }}的形式显示在html模板页中,图片该怎么显示呢?
img src={{ xx.图片字段 }}...>肯定不行,源码显示为:<GridFSProxy:%20018560.jpg> .net中的基本思路是建一个一般处理程序页,将流输出为图片,再作为src属性,flask或Python中没写过,有谁能指点一下?
迷茫2017-04-22 09:00:52
Another way to solve the problem is: through src="/img/{{xx.image field.grid_id}}/" Defined as a public View Get the actual image stored through GridFS through the passed string objectid and output
from flask import Response
from bson.objectid import ObjectId
from mongoengine import *
app.route('/img/<oid>/')
def get_img(oid=None):
if oid:
proxy = GridFSProxy(grid_id=ObjectId(oid))
return Response(proxy.read(),mimetype='image/jpeg')
高洛峰2017-04-22 09:00:52
<img src="data:image/jpeg;base64,{{xx.图片字段base64编码}}" />
Similarly applies to other encodings and formats, but note that older browsers do not support this method.