views:

23

answers:

2

I have a controller (Spring-MVC) that generates a PDF report on a get request. The controller sets headers like "application/OCTET-STREAM", "Content-Disposition", "attachment; filename=\"" + filename + "\"" so that the user gets the save/open dialog forced to them. The controller gets a pdf generated and writes the pdf bytes directly to the response's output stream. The ModelAndView that is returned is null.

The issue is I need/want to disable the button that generates the report request after it is clicked and then re-enable it when the report is finished in order to prevent double clicking.

I thought I could use prototype.org's Ajax.Request to do this... This bit of code does work the buttons properly but then I'm stuck because the client never gets the pdf. I'm guessing I need to do something with the response but I don't know what. Any ideas/help is appreciated.

function displayPdf(button_b, pdf_url) {
   button_b.disabled = true;
   new Ajax.Request(pdf_url, {
       asynchronous:true,
       evalScripts:true,
       method:'get',
       onComplete: function(response) {
         button_b.disabled = false;
      },
      onFailure: function() {
          redAlert('crap');
     }
   });
}
A: 

My guess is you just want to prevent double clicking of the button. Why not just use a setTimeout to enable the button a few seconds after its been clicked?

setTimeout() example

so at the start of your desplayPdf function, you might put:

setTimeout("button-b.disabled = false",4000);

This would disable the button for 4 seconds after its clicked, and then enable it.

Erik
Yes - i just want to disable double clicking. The setTimeout was an option I considered but I wanted something a bit more accurate since the report generation time can vary. Plus I was just plain curious if it could be done.
BabaBooey
I don't think you can do exactly what you want, but both my suggestion and Chibu's should give you workable alternatives.
Erik
A: 

The best way I can think to accomplish everything that you want, assuming that you're still forcing a download would to be something like this I think:

<html>
<head>
    <script>
        function get_it() {
            document.getElementById('the_button').disabled = true;
            document.getElementById('the_frame').src = "http://www.ca3.uscourts.gov/opinarch/063575p.pdf";
        }
        function enable_button() {
            document.getElementById('the_button').disabled = false; 
        }
    </script>
</head>
<body>
    <iframe id='the_frame' onload='enable_button()'></iframe>
    <button id='the_button' onclick='get_it()'>Get it</button>
</body>
</html>

Basically, you're telling it to send it to the iframe instead of using an AJAX request. Once the pdf is ready, the iframe's onload event will fire, re-enabling the button. You'll probably want to use actual event listeners for this as opposed to the deprecated inline ones that I've used in the example :P

Chibu
I can't vote this up since I'm a noob but this is a fair solution. thanks :D
BabaBooey