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?

2 Answers 2

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
        }
    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
I don't think your answer addresses my problem, unless you can describe how to handle the attachment-result.
success(data, textStatus, jqXHR) You could receive data and handle it with jQuery. Just change the anonymous function to 3 arguments.
It appears that it is not possible to handle an attachment (download) response in ajax. [stackoverflow.com/questions/7464665/…. So, unfortunately, I'm still stuck.
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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.