title: How to create and Download Excel File by using Ajax request
body: <p>I want to create an Excel File, if the user pressed a formular button on my webpage. My first solution works fine. The User pressed the button, the serverside java code creates the file and after the java code was executed the webbrowser open a dialog, which aks the user whether to show or to save the excel file. </p>
  
  <p>But I get some new requirements from my customer. If the user pressed the formular button, it is necessary to show a animated gif image while the server side java code was executed and the animated gif must be disapeared immediately, if the server side java code was finished.</p>
  
  <p>This is the Struts2 submit Button, which starts the HTTP Request, if the button was pressed:    </p>
  
  <pre><code>&lt;s:submit value="show Data" onclick="myJsFunction(); return false;" /&gt;
  </code></pre>
  
  <p>This is the client code, which creates and sends the HTTP Request to my Struts2 Actionclass:    </p>
  
  <pre><code>function myJsFunction(){
       $.ajax({
                type: "POST",
                dataType: 'binary',
                url: "myActionClass.action",
                data: {//some necessary input values},                         
                success: function(data){                                   
                            console.log(data);
                            // js code to disappeared the animated gif image                          
                         }
        });               
   }
  </code></pre>
  
  <p>This is the Java code, which create the Binarycode:</p>
  
  <pre><code>private void returnExcelFileAsStream(final String filename) throws IOException {
      final InputStream is = new FileInputStream(filename);
      OutputStream os = null;
      try {
          response.setContentType("application/pdf");         
          response.setHeader("Cache-Control", "maxage=3600");
          response.setHeader("Pragma", "public");
          response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");         
          os = response.getOutputStream();
          int len;
          byte buf[] = new byte[1024];
          while ((len = is.read(buf)) &gt; 0) {
              os.write(buf, 0, len);
          }
          is.close();
          os.close();
       }
       catch(Exception e){
           // some exeception handling
       }
  }
  </code></pre>
  
  <p>This is the Struts2 execute method, which called the method returnExcelFileAsStream. </p>
  
  <pre><code>public String execute(){
  
      // some Java code
      returnExcelFileAsStream("MyExcelFile.xlsx")
  
      return null;
  }
  </code></pre>
  
  <p>This is my struts.xml file:     </p>
  
  <pre><code>&lt;action name="myActionClass" class="myPackage.myActionClass"&gt;
          &lt;result name="input" type="redirectAction"&gt;/WEB-INF/base/jsp/myJspPage.jsp&lt;/result&gt;
  &lt;/action&gt;
  </code></pre>
  
  <p>Now a have the problem, that the webbrowser doesn´t open a user dialog, which asked the user to show or to save the file. With firebug I see some cryptical characters in the HTTP Response. How can I solve this problem? </p>