Using CSVHelper on file upload

后端 未结 2 1221
傲寒
傲寒 2021-01-04 12:54

I am trying to use the CSVhelper plugin to read an uploaded CSV file. Here is my modelBinder class:

public class SurveyEmailListModelsModelBinder : DefaultMo         


        
相关标签:
2条回答
  • 2021-01-04 13:20

    I had similar issue , but sorted out, when I tried the below code

    void Main()
    {
        using (var reader = new StreamReader("path\\to\\file.csv"))
        using (var csv = new CsvReader(reader, 
               System.Globalization.CultureInfo.CreateSpecificCulture("enUS")))
        {
            var records = csv.GetRecords<Foo>();
        }
    }
    

    Please note below code wont work with latest versions of CSV Helper

    using (var csvReader = new CsvReader(reader))
    
    0 讨论(0)
  • 2021-01-04 13:21

    Not used the plugin but the error message seems pretty clear. There must be a Read() function to call before accessing the results. Try changing your code to something like this:

    using (var reader = new StreamReader(file.InputStream))
    using (var csvReader = new CsvReader(reader))
    {
        // Use While(csvReader.Read()); if you want to read all the rows in the records)
        csvReader.Read();
        return csvReader.GetRecords<SurveyEmailListModels>().ToArray();
    }
    
    0 讨论(0)
提交回复
热议问题