簡體   English   中英

如何創建 CSV 文件下載 Azure Function 在 ZA7F5F35426B927411FC9231B736

[英]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.

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