PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, August 27, 2022

[FIXED] How to Create CSV File download with Azure Function in Python

 August 27, 2022     azure, azure-functions, csv, export-to-csv, python     No comments   

Issue

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.

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.

I would like the downloaded file to have the name report.csv. Does anyone here have a solution?

Translated with www.DeepL.com/Translator (free version)


Solution

Try by setting Content-Disposition response 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')


Answered By - Gaurav Mantri
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing