flask+mongoengine做一小站,model中使用FileField字段類型存儲(chǔ)上傳的圖片到文檔,該文檔記錄其它文本字段已經(jīng)以{{ xx.字段 }}的形式顯示在html模板頁(yè)中,圖片該怎么顯示呢?
img src={{ xx.圖片字段 }}...>肯定不行,源碼顯示為:<GridFSProxy:%20018560.jpg> .net中的基本思路是建一個(gè)一般處理程序頁(yè),將流輸出為圖片,再作為src屬性,flask或Python中沒(méi)寫(xiě)過(guò),有誰(shuí)能指點(diǎn)一下?
走同樣的路,發(fā)現(xiàn)不同的人生
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')
<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.