I am trying to export a list to a CSV file. I brought everything to the point that I want to write the file to the response stream. It does nothing.
Here is my code:
Call the method from the page.
$('#btn_export').click(function () { $.post('NewsLetter/Export'); });
The code in the controller is as follows:
[HttpPost] public void Export() { try { var filter = Session[FilterSessionKey] != null ? Session[FilterSessionKey] as SubscriberFilter : new SubscriberFilter(); var predicate = _subscriberService.BuildPredicate(filter); var compiledPredicate = predicate.Compile(); var filterRecords = _subscriberService.GetSubscribersInGroup().Where(x => !x.IsDeleted).AsEnumerable().Where(compiledPredicate).GroupBy(s => s.Subscriber.EmailAddress).OrderBy(x => x.Key); ExportAsCSV(filterRecords); } catch (Exception exception) { Logger.WriteLog(LogLevel.Error, exception); } } private void ExportAsCSV(IEnumerable<IGrouping<String, SubscriberInGroup>> filterRecords) { var sw = new StringWriter();
But after Response.Write(sw) nothing happens. Is it possible to save the file this way?
respectfully
Edit
The response headers that I see when I click the button are as follows:
HTTP/1.1 200 OK Cache-Control: private Content-Type: text/csv; charset=utf-8 Server: Microsoft-IIS/7.5 X-AspNetMvc-Version: 2.0 Content-Disposition: attachment; filename=adressenbestand.csv X-Powered-By: ASP.NET Date: Wed, 12 Jan 2011 13:05:42 GMT Content-Length: 113
Which seems good to me ..
Edit
I got rid of the jQuery part that replaced it with a hyperlink, and now this works fine for me:
<a class="export" href="NewsLetter/Export">exporteren</a>
jquery c # asp.net-mvc csv
Gerard Jan 12 '11 at 12:40 2011-01-12 12:40
source share