Callback function for location.href

We have a code

JavaScript:

function Download() {
            //Show Loading Spinner
            location.href = "FileDownloadHandler.ashx";              
            //Some Code(Hiding Loading Spinner)
        }

aspx page:

<input type="button" value="Download" onclick="Download();" />

"FileDownloadHandler" will download the file to the user.

Actual result: The code below location.href is executed immediately without completion of the handler.

Expected Result: I want to stop the execution of "SomeCode" until the handler completes the execution.

How can i achieve this?

Thanks in advance.

+4
source share
2 answers

Finally, I found a solution to my problem. Using the jQuery plugin, we can achieve this. I implemented it and worked like a charm.

, , .

location.href FileDownloadHandler, .

 $("#loading").show();

 $.fileDownload('FileDownloadHandler.ashx', {

                successCallback: function (url) {

                    $("#loading").hide();

                },
                failCallback: function (html, url) {

                    $("#loading").hide();
                    alert("Failed");

                }
            });

http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/

http://jqueryfiledownload.apphb.com/

http://github.com/johnculviner/jquery.fileDownload/blob/master/src/Scripts/jquery.fileDownload.js

+2

Jquery get: https://api.jquery.com/jquery.get/

$.get("FileDownloadHandler.ashx", function() {
    alert("Called Ashx Page");
})
.done(function() {
    alert("Stuff to do after ashx");
})
0

All Articles