3 回答

TA貢獻1785條經驗 獲得超4個贊
用數據創建一個響應對象,然后設置內容類型標題。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)

TA貢獻1886條經驗 獲得超2個贊
假設我已經存儲了圖像路徑。以下代碼有助于發送圖像。
from flask import send_file
@app.route('/get_image')
def get_image():
filename = 'uploads\\123.jpg'
return send_file(filename, mimetype='image/jpg')
uploads是我的文件夾名稱,其中包含123.jpg的圖像。
[PS:上載文件夾應位于腳本文件的當前目錄中]
希望能幫助到你。
添加回答
舉報