search

Home  >  Q&A  >  body text

flask - <img src如何指向并一个mongodb 的图片字段?

flask+mongoengine做一小站,model中使用FileField字段类型存储上传的图片到文档,该文档记录其它文本字段已经以{{ xx.字段 }}的形式显示在html模板页中,图片该怎么显示呢?

img src={{ xx.图片字段 }}...>肯定不行,源码显示为:<GridFSProxy:%20018560.jpg> .net中的基本思路是建一个一般处理程序页,将流输出为图片,再作为src属性,flask或Python中没写过,有谁能指点一下?

怪我咯怪我咯2803 days ago802

reply all(3)I'll reply

  • 迷茫

    迷茫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')
    

    reply
    0
  • 迷茫

    迷茫2017-04-22 09:00:52

    nginx+mongodbgridfs plug-in. No?

    reply
    0
  • 高洛峰

    高洛峰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.

    reply
    0
  • Cancelreply