0

i am trying to create a csv file from a string. I was able to create the csv file as string like so:

private void SaveToCsv<T>(List<T> reportData)

    {
        var lines = new List<string>();
        IEnumerable<PropertyDescriptor> props = TypeDescriptor.GetProperties(typeof(T)).OfType<PropertyDescriptor>();
        var header = string.Join(",", props.ToList().Select(x => x.Name));
        lines.Add(header);
        var valueLines = reportData.Select(row => string.Join(",", header.Split(',').Select(a => row.GetType().GetProperty(a).GetValue(row, null))));
        lines.AddRange(valueLines);

    }

But I cannot seem to find out how to create an actual .csv file from the string in lines, as the function app cannot follow a specific path (eg: D://drive/user/xya)

How do I create a file in code without a path in a function app?

innom
  • 770
  • 4
  • 19
  • 1
    Have you tried dumping the CSV file to an Azure File/Blob Storage or something similar? – Mithgroth Feb 28 '22 at 10:36
  • I red about this, but I wasnt sure if I needed that. Can you elaborate further? – innom Feb 28 '22 at 10:41
  • Azure Functions are considered "serverless", so they are just pieces of code that runs on a "somewhat random" environment. You should take your storage concerns elsewhere like Blob Storage or File Storage. Your Azure Function is just a method that'll run in some machine, where you don't really care about its filesystem. – Mithgroth Feb 28 '22 at 10:44

1 Answers1

0

Please check if the below steps helps to work around:

  • After generating the CSV Files, to store them you need the storage account. It is automatically created when creating the Azure Function App.

  • Update the Storage account name, connection string and file path in local.settings.json as well as in the configuration of the Azure Function App in the portal.

  • Here File_path is the blob container folder path where you're going to save the CSV files.

  • Add the CORS option through portal or code to run out of the issues like allowing the requests from your local IP addresses, current IP address if connected to VPN.

  • Refer this article for practical workaround and the code.

  • If you want to save the CSV files in the temporary location of the Azure Functions, then refer this SO Thread

References:

  1. Storage Consideration of Azure Functions
  2. To mount Azure local storage as a local file, refer to this MSFT Q&A
  3. Accessing Azure File Storage from Azure Function