我将一个来自csv文件的Stream从我的Controller传递到我的业务层进行处理。流使得方法没问题但是只要我声明我的TextFieldParser并传入我的流,数据就会消失,因此不会被处理。
public CsvRecordReportModel processCsvStream(Stream dataStream, RecordSource recordSource, string fileName)
{
//Create instance of the report.
var report = new CsvRecordReportModel();
report.InsertedRecordCount = 0;
report.FileName = fileName;
using (TextFieldParser csvParser = new TextFieldParser(dataStream))
{
csvParser.CommentTokens = new string[] {"#"};
csvParser.SetDelimiters(new string[] {","});
csvParser.HasFieldsEnclosedInQuotes = true;
// Skip the row with the column names
csvParser.ReadLine();
while (!csvParser.EndOfData)
{
//Do stuff
}
}
}