Search code examples
asp.net-mvccsvfilehelpers

Filehelpers reading data from external source


I am trying to read an external source CSV file with filehelpers.

Here is my code:

var engine = new FileHelperAsyncEngine<FileController>();

        using (engine.BeginReadFile("https://dl.dropboxusercontent.com/s/xxxyyyzzz/data.csv"))
        {
            foreach(FileController csvData in engine)
            {
                Console.WriteLine(csvData.batteryLevel);
            }
        }

engine.BeginReadFile not working with http links. How can I read this file with Filehelpers?


Solution

  • I don't think FileHelpers works over http. But you can download it first:

    var engine = new FileHelperAsyncEngine<FileController>();
    
        string fileName = DownloadFile("https://dl.dropboxusercontent.com/s/xxxyyyzzz/data.csv");
        using (engine.BeginReadFile(fileName))
        {
            foreach(FileController csvData in engine)
            {
                Console.WriteLine(csvData.batteryLevel);
            }
        }
        File.Delete(fileName);
    
    
    public static string DownloadFile(Uri url)
    {
        string fileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("n") + ".csv");
        WebClient aWebClient = new WebClient();
        aWebClient.DownloadFile(url, fileName);
        return fileName;
    }