[英]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. 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.
The CSV file is already successfully created and downloaded. CSV 文件已成功创建和下载。
This is solved with the following code:这可以通过以下代码解决:
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')
The problem is that the file that is downloaded has the name of the HttpTrigger and no file extension.问题是下载的文件具有 HttpTrigger 的名称并且没有文件扩展名。
I would like the downloaded file to have the name report.csv.我希望下载的文件名为 report.csv。 Does anyone here have a solution?
这里有人有解决方案吗?
Translated with www.DeepL.com/Translator (free version)使用www.DeepL.com/Translator翻译(免费版)
Try by setting Content-Disposition
response header.尝试设置
Content-Disposition
响应 header。
Something like:就像是:
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.