2

I am LinqToCsv and this tutorial to convert my data to a downloadable csv files.

WHAT HAVE I DONE :

I have created an action like the one shown below:

public FileResult GetReportCsvData(MyFilterModel model)
{
    var data = _myService.GetMyData(model);

    var fileName = String.Format("{0}_{1}_{2:yyyy_MMM_d}", Helpers.GetLoggedInUserName(), "Ad-Reports", DateTime.Today) + ".csv";
    var outputFileDescription = new CsvFileDescription { SeparatorChar = ',', FirstLineHasColumnNames = true };
    
    var cc = new CsvContext();
    cc.Write(data, "c:/" + fileName, outputFileDescription);

    return File("c:/" + fileName, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}

and using the following jquery I get my csv file:

exportCsv: function () {
    var data = this.getFilters();
    var url = '/MyController/GetReportCsvData';
    var form = document.createElement('form');
    form.action = url;
    form.method = 'POST';
    form.style.display = 'none';        
    for (i in data) {
        if (data[i] != "") {
            var inputElement = document.createElement('textarea');
            inputElement.name = i;
            inputElement.value = data[i];
            form.appendChild(inputElement);
        }
    }
    document.body.appendChild(form);
    form.submit();
}

This works and I get the csv file with the download prompt.

WHAT DO I WANT

Here with the current code I have to save the file on my server (C:/filename) and then return its File object.

I do not want to save it on my server.

What changes do I have to make to achieve this ?

Community
  • 1
  • 1
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281

1 Answers1

-1

on "cc.Write" you write to disk, outputFileDescription is already a TextStream. So instead of saving it to disk first, and then reading it from disk with "File", just return the TextStream

user3036342
  • 1,023
  • 7
  • 15
  • @Kodefoo Why? Google can serve up 10's of thousands of examples on how to read a textstream without saving it to disk first and reading it from disk again? – user3036342 Dec 03 '14 at 10:15
  • 1
    Then it should be no problem for you to add to your answer and make it that much more helpful to other people who end up here from a google search. – Paul Parker Dec 07 '15 at 21:53