用数据创建一个响应对象,然后设置内容类型标题。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.Headers和flask.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)