1

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?

user2687506
  • 789
  • 6
  • 21
  • Plesse see http://stackoverflow.com/a/10852040/3805983 you can add target="_blank" to force a new tab. – afnpires Jun 23 '16 at 12:00
  • Yes, the problem wasn't to open in a new tab, unfortunatley. The problem is that when my controller is working, even though it's async and it's during the "await"-phase, the original tab still can't do anything. I can't press a link for example, everything just locks up until the file is downloaded. Sorry if I was unclear in my original question. – user2687506 Jun 23 '16 at 12:16
  • An answer on another thread - http://stackoverflow.com/questions/22830393/ienumerablestring-to-stream-for-filestreamresult#22834363 - worked for me. – Andy Nichols Jun 23 '16 at 12:25
  • 1
    @user2687506: I suspect your problem is due to the ASP.NET session. Try disabling session state. – Stephen Cleary Jun 23 '16 at 15:21
  • @StephenCleary you are correct! Thank you very much! For others with the same problem, look at this stackoverflow-answer, but use `[SessionState(SessionStateBehavior.ReadOnly)]`: http://stackoverflow.com/a/4235006/2687506 – user2687506 Jun 27 '16 at 09:08
  • @StephenCleary - can u please explain what is reason behind this. Why we need to make session state as Readonly ? – Kumar Sep 20 '17 at 09:45
  • 2
    @Coder: When session state is the default (read/write), ASP.NET has to ensure that it's not written to simultaneously from multiple requests. So, it only lets one request for that session execute at a time. – Stephen Cleary Sep 20 '17 at 15:54
  • @StephenCleary +1 Thanks. – Kumar Sep 21 '17 at 12:54

0 Answers0