Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Using CsvHelper with a Stream

$
0
0

I'm trying to use CsvHelper to read in a CSV file and create a DataTable from it. The first row will be a header record providing column names, but other than that, the structure of the file is unknown. If I use the following code (taken from an example by the author of CsvHelper), it works.

using (var reader = new StreamReader("path\\to\\file.csv"))using (var csv = new CsvReader(reader)){    // Do any configuration to `CsvReader` before creating CsvDataReader.    using (var dr = new CsvDataReader(csv))    {                var dt = new DataTable();        dt.Load(dr);    }}

However, if I use an alternate constructor for the StreamReader that takes a Stream as a parameter instead of a file path, then the creation of the CsvDataReader fails with an error message of "Synchronous reads are not supported."

I've tried a few other methods of CsvHelper in attempts to handle the data differently, but I keep running into the same error any time that the StreamReader is created by passing in a Stream rather than a file path. I'm starting to wonder whether the real issue lies with the implementation of StreamReader or with CsvHelper. Passing in a Stream makes much more sense in my situation (a Blazor Server app). Any ideas?

EDIT:

I believe David Specht is correct in that there is something unique about the particular stream I was using. In further testing, I've found that some strings do work. In the situation where I have the error, I am reading the stream from Steve Sanderson's BlazorInputFile component (on GitHub) using the IFileListEntry.Data stream interface. I suspect that there is something in its implementation that causes the error I'm getting. If that's the case, then a workaround would be helpful. (Maybe creating one stream from another to switch between asynchronous and synchronous streams? Not sure how to do that yet, but I'm going to give it a shot.)


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>