2

I have a situation where I create a form dynamically and submit to my server-side controller so I can query my database and return a csv file. I need to be able to put up an overlay so the user can't hammer the submit button and send 10 requests to get the file back.

Because of this I CANNOT use ajax since ajax will not allow me to send back a file for the browser to automatically detect and ask the user to download.

showSpinner();

    var $form = $("<form/>")
        .addClass("hidden")
        .attr("accept", "application/json")
        .attr("action", "/myPath")
        .attr("method", "POST")
        .appendTo("body");

    $form.bind('ajax:complete', function() {

        hideSpinner();

    });

    $form.submit();

"ajax:complete" was just something I was trying with no luck. I also tried putting the hideSpinner right after the .submit(). But that just hides the spinner immediately since .submit() seems to be async.

Keith.Abramo
  • 6,952
  • 2
  • 32
  • 46
  • You're doing what now ? – adeneo Sep 23 '13 at 19:41
  • Am I missing something? I don't see any ajax, just a normal form submit. – Erik Philips Sep 23 '13 at 19:42
  • 2
    it's done with cookies, when you send file from server, you set cookie – karaxuna Sep 23 '13 at 19:43
  • 1
    He's submitting a form that returns a download. After the download starts he will still be on the form, at which point he wants to remove the overlay. there is no event for you to listen for, therefore cookies as @karaxuna suggests is pretty much the only way, aside from using ajax or iframes. – Kevin B Sep 23 '13 at 19:44
  • you can use ajax to fetch files to download in all current browsers. – dandavis Sep 23 '13 at 19:45
  • 1
    I see, then it's a duplicate of [jQuery how to trigger event after form post returns?](http://stackoverflow.com/questions/13529752/jquery-how-to-trigger-event-after-form-post-returns) – adeneo Sep 23 '13 at 19:46
  • I construct the file dynamically on the server and write it to the response output stream. I don't hit the file directly which would work for an ajax if I could hit the file directly. – Keith.Abramo Sep 23 '13 at 19:48
  • http://stackoverflow.com/a/9970672/1002936 Maybe this can help? – bvx89 Sep 23 '13 at 19:49
  • Another option: you can turn the click event on and off when you don't want the form to submit. Then you can still use ajax: $('#submitbutton').on('click.mynamespace', function() { /* Do stuff */ }); $('#submitbutton').off('click.mynamespace'); – redconservatory Sep 23 '13 at 19:52
  • I'm going to try the iframe approach first and then the cookies if that fails. To make sure I'm clear on the cookies. I would set the cookie right before my server side function completes and then do a setInterval in javascript to look for that cookie? – Keith.Abramo Sep 23 '13 at 19:54
  • While the form is submitted, the browser would lock, did you try the onsubmit event, perhaps with a zeroed timeout to defer it until after the browser unlocks etc ? – adeneo Sep 23 '13 at 19:56
  • @adeneo onSubmit event fires before the form submits – Keith.Abramo Sep 23 '13 at 20:14
  • @Keith.Abramo right, but the zeroed timeout would cause the callback of the timeout to happen shortly after that. I suspect that the browser doesn't actually get locked while the download is happening, otherwise you wouldn't need an overlay to begin with. – Kevin B Sep 23 '13 at 20:17

0 Answers0