簡體   English   中英

如何使用 make_response 下載 Flask 中的文本文件?

[英]How can I download a text file in Flask using make_response?

我想在我的目錄中記錄一個文本文件以在 Flask 中使用 make_response,但是我寫下的代碼不起作用。

這是我寫下的使用 make_response 下載文本文件的代碼。

@api.route('/structure_voice_log', methods=['POST'])
def download():
    response = make_response("hogehoge")
    response.headers["Content-Disposition"] = "attachment; filename=outbound.txt"
    response.headers["Content-type"] = "text/plain"
    return response

這是我發出的命令。

curl -L -i -H "Content-Type: application/json" http://localhost:17354/structure_voice_log -X POST -d '{"voice_log": "hogehoge"}'

這是上述命令結束后的結果。

HTTP/1.0 200 OK
Content-type: text/plain
Content-Length: 8
Content-Disposition: attachment; filename=outbound.txt
Access-Control-Allow-Origin: *
Server: Werkzeug/0.15.5 Python/3.7.5
Date: Tue, 12 Nov 2019 16:15:44 GMT

hogehoge%

我得到的只是一個字符串“hogehoge”,而不是一個文本文件。 我怎樣才能做到? 使用這個框架我可能 go 錯了。 如果您能幫助我,我將不勝感激。

make_response接收第一個 arguments 一個字符串來顯示。 可以使用send_file function 代替( https://pythonise.com/series/learning-flask/sending-files-with-flask )。

希望能幫助到你。 此致

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM