第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Flask send_file() 返回正確的 .xlsx 數(shù)據(jù)但不正確的文件名

Flask send_file() 返回正確的 .xlsx 數(shù)據(jù)但不正確的文件名

慕桂英3389331 2022-01-05 13:12:52
我在 Google App Engine 標準實例中使用 Python 2.7 + flask 從存儲桶中獲取 .xlsx 文件。當我點擊我的下載路徑時,它返回正確的數(shù)據(jù),但文件名只是“下載”并且文件未被識別為 .xlsx 文件。我可以在 Excel 中打開文件,但是數(shù)據(jù)確實顯示正確。我嘗試將數(shù)據(jù)寫入 io.StringIO,然后使用該數(shù)據(jù)結(jié)構(gòu)調(diào)用 send_file,但它給了我與以前相同的問題。這是我的路線。@app.route('/download', methods=['GET'])def download():    run_id = request.args.get('run_id')    fp = BucketHelper().get_report_fp(run_id)    send_file(fp,             as_attachment=True,             mimetype='application/vnd.ms-excel',             attachment_filename="test.xlsx")這是獲取 cloudtorage.storage_api.ReadBuffer 對象的函數(shù)。import cloudstoragefrom google.appengine.api import app_identityclass BucketHelper:    def __init__(self):        self.bucket_path = '/my-bucket/path'    def get_report_fp(self, run_id):        filename = "{}/my_report_{}.xlsx".format(self.bucket_path, run_id)        return cloudstorage.open(filename, mode='rb')文件被命名為“下載”,而不是被命名為“test.xlsx”,并且不被識別為 Excel 文件。任何幫助表示贊賞。
查看完整描述

1 回答

?
Qyouu

TA貢獻1786條經(jīng)驗 獲得超11個贊

正在調(diào)用該文件,download因為這是您設(shè)置的路徑。


@app.route('/download', methods=['GET'])

def download():

如果您沒有能力控制用戶的請求,您應該能夠通過使用重定向來定義一個虛假的文件名,否則只需使用定義的新路由進行下載。


嘗試這樣的事情?


...

from flask import redirect

...


@app.route('/download', methods=['GET'])

def download_redirect():

    redirect('/download/test.xlsx')


@app.route('/download/<filename>', methods=['GET'])

def download(filename):


    run_id = request.args.get('run_id')

    fp = BucketHelper().get_report_fp(run_id)

    send_file(fp,

             as_attachment=True,

             mimetype='application/vnd.ms-excel',

             attachment_filename="test.xlsx")


查看完整回答
反對 回復 2022-01-05
  • 1 回答
  • 0 關(guān)注
  • 474 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號