2

My app does a form.submit() that invokes an action that returns a FileResult (an attachment). Prior to submit I have javacript that puts up a "please wait" div. I need to clear it when the result is returned. For actions that return a response that writes the page, I use the window.onload event to clear the div, but that event doesn't fire when an attachment is returned.

It doesn't work to do something like form.submit();HidePleaseWait() because submit is asynchronous. Is there some other event that fires when an attachment is returned? Or, is there some way to combine multiple ActionResult so that I could update the page in the same response?

Elroy Flynn
  • 3,032
  • 1
  • 24
  • 33

2 Answers2

1

If you are using jQuery you should do the following.

$('selector').submit(function(event){
    event.preventDefault();
    // @Hiding
    // Do your AJAX call
    $.ajax({
        url: "test.html",
        success: function(){
            // @What to do when done
        }
    });
});
Fabián Heredia Montiel
  • 1,687
  • 1
  • 16
  • 30
  • What would I do in the "what to do when done" block? I know I can hide my pleasewait div, but I'd also have to handle the attachment response. I know how to handle "normal" responses that would have me, for example, replace content, but I don't know how to handle this. – Elroy Flynn Jan 09 '12 at 02:20
  • Well, if you want to send a form WITHOUT reloading the page you would change it to an AJAX call. It avoid the browser from moving into the action address. – Fabián Heredia Montiel Jan 09 '12 at 02:46
  • I don't think your answer addresses my problem, unless you can describe how to handle the attachment-result. – Elroy Flynn Jan 09 '12 at 02:55
  • success(data, textStatus, jqXHR) You could receive data and handle it with jQuery. Just change the anonymous function to 3 arguments. – Fabián Heredia Montiel Jan 09 '12 at 02:58
  • It appears that it is not possible to handle an attachment (download) response in ajax. [http://stackoverflow.com/questions/7464665/ajax-response-content-disposition-attachment]. So, unfortunately, I'm still stuck. – Elroy Flynn Jan 09 '12 at 03:11
0

I know it's a old post, but I had the same need and found something that might interrest someone in the future.

I found the solution there : https://www.codeproject.com/Articles/1095434/Show-an-animation-while-waiting-for-a-download-to

I just had to add window.clearInterval(_tmr) to stop the verification each 100ms and document.cookie = 'dlc= ; expires = Thu, 01 Jan 1970 00:00:00 GMT' to clear the cookie.

So my final code looks like :

var _tmr;

$('form').submit(function () {
    if (!_tmr) {
        $('#cookieValue').val(Date.now().toString());

        _tmr = window.setInterval(VerifyEnd, 100);
    }
});

function VerifyEnd() {
    var _str = 'dlc=' + $('#cookieValue').val();
    if (document.cookie.indexOf(_str) !== -1) {
        $('body').removeClass("loading");
        window.clearInterval(_tmr);
        document.cookie = 'dlc= ; expires = Thu, 01 Jan 1970 00:00:00 GMT';
        _tmr = null;
    }
}