您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Flask返回存储在数据库中的图像

Flask返回存储在数据库中的图像

用数据创建一个响应对象,然后设置内容类型标题attachment如果希望浏览器保存文件不显示文件,则将内容处置标题设置为。

@app.route('/images/<int:pid>.jpg')
def get_image(pid):
    image_binary = read_image(pid)
    response = make_response(image_binary)
    response.headers.set('Content-Type', 'image/jpeg')
    response.headers.set(
        'Content-Disposition', 'attachment', filename='%s.jpg' % pid)
    return response

相关:werkzeug.Headersflask.Response

您可以将类似文件的Oject和header参数传递send_file给它,以设置完整的响应。使用io.BytesIO二进制数据:

return send_file(
    io.BytesIO(image_binary),
    mimetype='image/jpeg',
    as_attachment=True,
    attachment_filename='%s.jpg' % pid)
Python 2022/1/1 18:29:32 有188人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶