亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

燒瓶返回存儲在數據庫中的圖像

燒瓶返回存儲在數據庫中的圖像

幕布斯6054654 2019-12-26 10:31:00
我的圖像存儲在MongoDB中,我想將它們返回給客戶端,代碼如下:@app.route("/images/<int:pid>.jpg")def getImage(pid):    # get image binary from MongoDB, which is bson.Binary type    return image_binary但是,我似乎無法直接在Flask中返回二進制文件?到目前為止,我的想法是:返回base64圖像二進制文件的。問題是IE <8不支持此功能。創建一個臨時文件,然后使用返回send_file。有更好的解決方案嗎?
查看完整描述

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)


查看完整回答
反對 回復 2019-12-26
?
MM們

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:上載文件夾應位于腳本文件的當前目錄中]


希望能幫助到你。


查看完整回答
反對 回復 2019-12-26
  • 3 回答
  • 0 關注
  • 386 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號