[英]Download file, parse it and serve in Flask
我正在與Flask邁出第一步。 我可以成功地從客戶端下載文件,然后從這里返回代碼: http : //flask.pocoo.org/docs/patterns/fileuploads/
但是如何改變它(例如一行一行)然后將其提供給客戶?
我可以在以下后面用read()
獲取字符串:
if file and allowed_file(file.filename):
然后處理它。 所以問題實際上是:我如何將輸出字符串作為文件提供?
我根本不想將它保存在服務器的硬盤上(原始版本和更改版本)。
您可以使用make_response
為您的字符串創建響應並添加Content-Disposition: attachment; filename=anyNameHere.txt
在返回之前, Content-Disposition: attachment; filename=anyNameHere.txt
:
@app.route("/transform-file", methods=["POST"])
def transform():
# Check for valid file and assign it to `inbound_file`
data = inbound_file.read()
data = data.replace("A", "Z")
response = make_response(data)
response.headers["Content-Disposition"] = "attachment; filename=outbound.txt"
return response
另請參閱:有關流媒體內容的文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.