1

I am using MVC 4.

I have a method in my controller which generates a CSV file on demand and I want this file to be then downloaded by the user without the need of saving it to the disk at the server side. So I am passing a MemoryStream on the File() object to avoid having to firstly save file to disk and later give him the path for download.

Controller Method:

[HttpGet]
public ActionResult GenerateCsv(string data)


    var sb = new StringBuilder();
    sb = GetCsvReportToString(data);

    var stream = new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()));

    return this.File(stream, "text/csv", "ReportCsv.csv");
}

sb (StringBuilder) has the correct data, as I have debugged it and confirmed it.

my javascript code:

window.open(generateFileLink + '?data=' + dataToSend, '_blank');

The window gets in fact opened, the download dialog appears but with an error, saying that it cannot be downloaded from localhost:

enter image description here

The name is different in the download "ReportToCsv" because I didn't use real names for methods in my question, but doesn't matter.

Can anyone assist?

Erik Philips
  • 48,663
  • 7
  • 112
  • 142
filipehd
  • 820
  • 3
  • 13
  • 30
  • 3
    If you want to black out the "ServerFilesCompare" part of the file name you'll need to do it on the dialog in the background as well. –  May 09 '14 at 14:03
  • You don't need to put the Bytes[] into a MemoryStream. The File Object can Take the Byte Array as it is. And shouldn't it be HttpPost instead of HttpGet? – Ralf May 09 '14 at 14:12
  • @DaveParsons that in fact really helped me solving my problem :) – filipehd May 09 '14 at 14:19
  • errrr..... glad I could help?! –  May 09 '14 at 14:20
  • @Ralf it should be a Get, because i am passing the parameter through the URL. So it's a GET. Well I don't NEED to, but that doesn't really make a difference here :( – filipehd May 09 '14 at 14:21
  • Have you tried this suggestion by adding Cache-Control header - http://stackoverflow.com/questions/16846054/asp-net-mvc-3-file-download-not-working-in-ie8 – ramiramilu May 09 '14 at 14:40
  • @filipehd: if you have solved the problem it would be good if you could share the answer with us all - if you feel it's too trivial etc then you can always delete the question. –  May 09 '14 at 15:26
  • @DaveParsons I was being ironic... I was just commenting that your comment was just useless and didn't help in anything. I asked the community for help and you answered me that I forgot to black out the ServerFilesCompare... – filipehd May 09 '14 at 15:42

2 Answers2

0

Try this

var sb = new StringBuilder();
            sb=GetCsvReportToString(data);
            return File(Encoding.UTF8.GetBytes(sb.ToString()), System.Net.Mime.MediaTypeNames.Application.Octet, "ReportCsv.csv");
malkam
  • 2,235
  • 1
  • 12
  • 17
  • Didn't make any difference. Still getting the same error. I will try @ramiramilu solution. It may be in fact a problem with the Headers because my solution works on Chrome – filipehd May 12 '14 at 11:19
  • @filipehd:Actually above code worked fine with IE 10 in my system. Not sure why you are getting error. – malkam May 12 '14 at 11:52
  • the error only occurs in IE versions 6-8. So it both my code and yours work on IE10 :) – filipehd May 12 '14 at 13:57
0

The answer to this question can be found here: https://stackoverflow.com/a/11267754/1417487

It's a regarding the headers of cache, for IE versions 6-8.

Community
  • 1
  • 1
filipehd
  • 820
  • 3
  • 13
  • 30