title: Disabling/Enabling Submit Button pending Struts File-Download Action
body: <p>I found some bits-and-pieces regarding disabling the Submit button for form processing (to avoid a double form submission), but it doesn't help my particular case:</p>
  
  <p>Suppose I disable 'Submit' in jQuery on form submission,</p>
  
  <p>JS/jQuery:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $(document).ready(function() {
  
           alert('In Document.ready');
  
          // Initially enable the Submit button
          $(this).find('input[type=submit]').prop('disabled', false);
  
          $('#form').on('submit', function(e){
              // Disable Submit button on form submit
              $(this).find('input[type=submit]').prop('disabled', true);
            });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>Form HTML</p>
  
  <pre><code>&lt;form id="form" method="post" action="/generate.do"&gt;
  
                      &lt;label&gt;Number&lt;/label&gt;&lt;br/&gt;
                      &lt;input type="text" name="number" maxlength="50" value="" title="Enter Number"&gt;
                      &lt;html:submit value="Go"/&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>This works -- the button gets disabled. I have a Struts Action class on the server-side that handles this form.</p>
  
  <p>The Struts Action result is to return a File Download. I write to the Response per the usual Struts guidelines and get my Save As dialog</p>
  
  <p>Struts Action:</p>
  
  <pre><code>InputStream is = getImageBytes();
  
  response.setContentType("application/pdf");
  response.addHeader("content-disposition", "attachment; filename=\"file.pdf\"");
  
  // Write to Response Stream
  OutputStream os = response.getOutputStream();
  byte[] buffer = new byte[4096];
  int length;
  while ((length = is.read(buffer)) &gt; 0)
  {
      os.write(buffer, 0, length);
  }
  is.close();
  os.flush();     
  // Response was handled within the action, so set forward = null
  forward = null;
  </code></pre>
  
  <p><strong>Problem:</strong> The Submit button doesn't get re-enabled after I come back from the Action, it's still disabled. I don't get my page-startup jQuery alert box on coming back, so the page hasn't been refreshed.</p>
  
  <p>I can't forward or sendRedirect anymore with this forward, I get these errors:</p>
  
  <p>For example,
  (1) Forward</p>
  
  <pre><code>java.lang.IllegalStateException: Cannot forward after response has been committed
      at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:328)
  </code></pre>
  
  <p>for</p>
  
  <pre><code>ActionForward forwardSuccess = mapping.findForward("success");
  forwardSuccess.setRedirect(false);
  return forwardSuccess;
  </code></pre>
  
  <p>(2) SendRedirect</p>
  
  <pre><code>java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
      at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:494)
  </code></pre>
  
  <p><strong>Goal:</strong> The goal is to return a File-Download result from the server-side action, but intercept it somehow in jQuery <strong>to re-enable the Submit button</strong> in the form when the result comes back.</p>