Filehelpers从外部源读取数据

时间:2015-11-25 23:05:42

标签: asp.net-mvc csv filehelpers

我正在尝试使用filehelpers读取外部源CSV文件。

这是我的代码:

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 无法使用http链接。如何使用Filehelpers读取此文件?

1 个答案:

答案 0 :(得分:3)

我认为FileHelpers不适用于http。但你可以先下载它:

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;
}