flask 从二进制数据返回图片

import io

from flask import current_app as app
from flask import send_file

from myproject import Obj

@app.route('/logo.png')
def logo():
    obj = Obj.objects.get(title='Logo')

    return send_file(io.BytesIO(obj.logo.read()),
                     attachment_filename='logo.jpg',
                     mimetype='image/jpg')

通过io.BytesIO 从二进制数据获取一个fd.
注意, 必须要有filename或者mimetype之一, 否则会报错.

    原文作者:EthanSun
    原文地址: https://segmentfault.com/a/1190000012528303
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞