I have created a controller that, through a stream, lets the user download a CSV-file, but when it's downloading, the user can't do anything else on the site.
The controller is:
public async Task ExportClientsListToCSV(string guid)
{
var search = Session[guid] as SearchVM;
StringWriter sw = new StringWriter();
sw.WriteLine("\"Name\",\"Status\",\"E-mail\",\"Phone\"");
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment;filename=Exported_Organizations.csv");
Response.ContentType = "text/csv";
Response.ContentEncoding = Encoding.UTF32;
var result = await _adminFacade.OrganizationAllSearchAsync(search.SearchQuery,
false,
false);
foreach (var line in result)
{
sw.WriteLine("\"{0}\",\"{1}\",\"{2}\",\"{3}\"",
line.OrganizationName, line.Status, line.OrgContactEmail, line.Phone);
}
Response.Write(sw.ToString());
Response.End();
}
Right now I solve it by Html.ActionLink("Export", "ExportClientsListToCSV", new {guid = Model.DownloadGuid})
but I've also tried with:
e.preventDefault();
$.ajax({
cache: false,
url: this.exportUrl,
data: this.$form.serialize()
})
.success(function(data) {
window.open(this.downloadUrl + '?guid=' + data.fileGuid, '_blank');
}.bind(this));
Which sure opened in a new window/tab, but the user couldn't do anything in the original tab until the file actually was downloaded. Is there anyway to work around this?