[英]How to Create CSV File download with Azure Function in Python
I am trying to write an Azure Function that fetches data from a WebAPI, and returns a CSV file as a download to the user who enters the Function URL.
CSV 文件已成功創建和下載。
這可以通過以下代碼解決:
import logging
import azure.functions as func
from init_iterateNodeUserHomeAdress import createCSV
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
#Geneartion CSV String
csv=createCSV()
#Converting String to CSV File
filebytes= bytes(csv, 'utf-8')
#Returning CSV file
return func.HttpResponse(body=filebytes, status_code=200, mimetype='application/octet-stream')
問題是下載的文件具有 HttpTrigger 的名稱並且沒有文件擴展名。
我希望下載的文件名為 report.csv。 這里有人有解決方案嗎?
使用www.DeepL.com/Translator翻譯(免費版)
嘗試設置Content-Disposition
響應 header。
就像是:
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
#Geneartion CSV String
csv=createCSV()
#Converting String to CSV File
filebytes= bytes(csv, 'utf-8')
#Returning CSV file
headers = {
"Content-Disposition": "attachment; filename='report.csv'"
}
return func.HttpResponse(body=filebytes, status_code=200, headers=headers, mimetype='application/octet-stream')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.