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之一, 否则会报错.

你可能感兴趣的:(flask 从二进制数据返回图片)