Writing CSV to MemoryStream using LinqToCSV does not return any data

asp.net-mvc-4, linq, memorystream

Solution

Make sure you reset stream position to 0. Also make sure you flush your `StreamWriter` before that.

Problem

I've verified using `System.Text.Encoding.ASCII.GetString(ms.ToArray));` that my memorystream has the expected data. However using the LinqToCSV nuget library will not generate my csv file. I get no errors or exceptions thrown. I just get an empty file when I'm prompted to open the file. Here is my Action Method ``` public FileStreamResult Export(){ var results = _service.GetProperties().Take(3); System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.IO.TextWriter txt = new System.IO.StreamWriter(ms); CsvFileDescription inputFileDescription = new CsvFileDescription{ SeparatorChar =',', FirstLineHasColumnNames = true } ; CsvContext csv = new CsvContext(); csv.Write(results,txt,inputFileDescription); return File(ms , "application/x-excel"); } ``` I find it interesting, if I change the return type to contentResult, and the return method to Content() and pass it `System.Text.Encoding.ASCII.GetString(ms.ToArray));` I do get a browser window showing my data.

Original source