Flask 如何把数据库存储的二进制图片返回给浏览器查看?


MongoDB 用 bson 存储二进制图片:


 @app.route("/img/<int:id>.png")
def getImg(id):
    return image_file

怎样把 bson 数据重新解析为图片并返回给浏览器查看?

flask python 二进制 mongodb

TDGUPZ 10 years, 2 months ago

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

请转账付费 answered 10 years, 2 months ago

Your Answer