I am trying to create a csv file using CSVHelpernuGet package
This is the code
public ActionResult Test()
{
var ms = new MemoryStream();
var sr = new StreamWriter(ms);
var csv = new CsvWriter(sr);
csv.WriteField("sd");
csv.WriteField("sd");
csv.WriteField("sd");
csv.WriteField("sd");
sr.Flush();
var len = ms.Length;
return File(ms, "text/csv", "test.csv");
}
However, the file is always empty.
I read here a few questions that suggest what StreamWriterneeds to be cleared. The position has moved to 0. However, Ive tried this and it doesn't seem to work
I also tried the same with the operators usingfor MemoryStream, StreamWriterand CSVWriter
I tried all this and it is still empty.
Moreover, the length is MemoryStreamalways zero
What am I doing wrong?
source
share