Mark Kadlec
Mark Kadlec

Reputation: 8460

Trigger javascript after FileResult return in ASP mvc

I have a Javascript call:

window.location.replace(instanceExtension(baseURL + "/AccountsReceivable/PrintStatementOfAccount?clientId=" + clientId, -1));

And the PrintStatementOfAccount() method takes a few seconds so I've added a mask to the page indicating that the PrintStatement is loading.

The ASP method is defined as:

public FileResult PrintStatementOfAccount(long clientId) { ... }

All works great, but I would like to disable the wait mask once the file returns. Any ideas on how I can achieve this?

Upvotes: 0

Views: 2180

Answers (2)

Vasile Tomoiaga
Vasile Tomoiaga

Reputation: 1747

You cannot know from JavaScript whether the file is returned to the browser.

As a workaround instead of returning the file directly you split the process:

  1. File is ready for download: reload the same page with a parameter that somehow identifies the file.
  2. On reloading you read that parameter and save it in a javascript value. Here you can hide the wait mask.
  3. In the page you check if the javascript file identifier is not null, and if not you make a GET request to the server for the file with javascript window.location='url_to_get_file'.

Upvotes: 1

dev
dev

Reputation: 78

You can pass token to the method and check for that token in the javascript.

var token = new Date().getTime();
$('#download_token_valueid').val(token); 
$.download(path + "Print.ashx", 'Id=' + id + "&token=" + token);    

fileDownloadCheckTimer = window.setInterval(function () {
        var cookieValue = $.cookie('fileDownloadToken');
        if (cookieValue == token)
            finishDownload();
    }, 1000);

function finishDownload() {
    window.clearInterval(fileDownloadCheckTimer);
    $.cookie('fileDownloadToken', null); //clears the cookie value
    $.unblockUI();
}   

Upvotes: 1

Related Questions