I'm trying to get a .csv file with some data filtered by the jlst object (which contains the selected ids of some dropdownboxes). Here is the ajax call:
$('#moduleCSVReport').click(function () {
$('#progress').show();
$.ajax({
url: '/Reports/GetPageAccessLevelsCSV?obj=' + jlst,
dataType: "json",
type: "GET",
success: function (data) {
$('#progress').hide();
}
});
});
And here's the function:
public ActionResult GetPageAccessLevelsCSV(string obj)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
ReportsSelectedItems cParams = jss.Deserialize<ReportsSelectedItems>(obj);
var locationIdFilter = (cParams.LocationId != 0) ? cParams.LocationId : 0;
var divisionIdFilter = (cParams.DivisionId != 0) ? cParams.DivisionId : 0;
IList<UserProfile> userProfiles = new List<UserProfile>();
userProfiles = Session.Query<UserProfile>().Where(n => n.Employee != null && ((n.Employee.Division.Id == cParams.DivisionId || cParams.DivisionId == 0) && (n.Employee.Location.Id == cParams.LocationId || cParams.LocationId == 0))).OrderBy(n => n.Employee.Name).ToList();
MemoryStream output = new MemoryStream();
StreamWriter writer = new StreamWriter(output, Encoding.UTF8);
writer.Write("Name,");
writer.Write("Team");
foreach(UserProfile user in userProfiles)
{
writer.Write(user.Employee.Name);
writer.Write(",");
writer.Write(HasAccessToModule((int)ModuleEnum.Team, user.Id).ToString());
writer.WriteLine();
}
writer.Flush();
output.Position = 0;
return File(output, "application/x-ms-excel", "test.csv");
}
I was following the breakpoints and the file seems to be successfully generated. It's just that I don't know how to display the file after receiving it in the ajax call. I presume there has to be a way to handle the file (in the success event maybe). If so, how can I do that? Thanks.