user3483118
user3483118

Reputation: 1

Executing JavaScript when FileResult returns

(using ASP.NET MVC) I am working on a page that generates an excel "report" on button click. The contents of the report depend on user selections, so it may take 5 seconds to generate, or it may take 30 seconds.

I am trying to figure out how execute browser-side code once the excel file is generated and begins downloading so that I can display a loading message and give them the option to generate additional reports.

When the user clicks the generate report button I am submitting a form:

$("#genReportBtn").click(function () {
  ...processing stuff...
  $("#genContractForm").submit();       
});

Which links to a FileResult type function on the server side:

public FileResult GenReport(...Variables...)
{
    ...Generating Excel File...

    return File(output.ToArray(),
        "application/vnd.ms-excel",
        "report.xls");
}

Which starts the download.

Is there any way to detect this return browser side?

Upvotes: 0

Views: 695

Answers (1)

Ross Bush
Ross Bush

Reputation: 15175

You will need to look into signalR to push progress to the client or use a technique that basically polls for progress. For example, I had to upgrade a report that rendered a pdf/excel file for an individual user to render the report for a wide selection of users. I was able to dissect the processing so that if the user requested 45 user reports then I could formulate a progress indicator that would update on the client for each of the 45 reports.

This was not trival for me. I used this article by Dino Esposito as a starting point. In a nutshell, it allows the client to poll a controller method on a timer for progress using a taskID to serialize access. I had to modify the code to fit my needs but it ended up working as expected.

Upvotes: 1

Related Questions