title: How to download excel file on ajax success
body: <p>I am downloading excel file on client side using ajax. File is downloaded but in encrypted code. I have tried so far is as below: </p>
  
  <p>Code on servlet side:    </p>
  
  <pre><code>response.setContentType("application/vnd.ms-excel");
  response.setHeader("Content-Disposition", "attachment; filename=ErrorDocument.xls");
  System.out.println("session get : " + (request.getSession().getAttribute("errorFile") instanceof Workbook));
  
  try {
      Workbook errorWorkbook = new HSSFWorkbook();
      errorWorkbook = (Workbook) request.getSession().getAttribute("errorFile");
      java.io.OutputStream out = response.getOutputStream();
      errorWorkbook.write(out);
      out.close();
  } 
  catch (Exception e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>JavaScript Code:</p>
  
  <pre><code>$.ajax({
      url:'errorFileDownload',
      method:'post',
      dataType: "text",
      success: function(response, status, xhr) {
          console.log("In success of file download.");
          if(response!=""){
              // check for a filename
              var filename = "";
              var disposition = xhr.getResponseHeader('Content-Disposition');
              if (disposition &amp;&amp; disposition.indexOf('attachment') !== -1) {
                  var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
                  var matches = filenameRegex.exec(disposition);
                  if (matches != null &amp;&amp; matches[1]) filename = matches[1].replace(/['"]/g, '');
              }
  
              var type = xhr.getResponseHeader('Content-Type');
              console.log("Content-type :" + type);
              var blob = new Blob([response], { type: type });
  
              if (typeof window.navigator.msSaveBlob !== 'undefined') {
                  // IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
                  window.navigator.msSaveBlob(blob, filename);
              } else {
                  var URL = window.URL || window.webkitURL;
                  var downloadUrl = URL.createObjectURL(blob);
  
                  if (filename) {
                      // use HTML5 a[download] attribute to specify filename
                      var a = document.createElement("a");
                      // safari doesn't support this yet
                      if (typeof a.download === 'undefined') {
                          window.location = downloadUrl;
                      } else {
                          a.href = downloadUrl;
                          a.download = filename;
                          document.body.appendChild(a);
                          a.click();
                      }
                  } else {
                      window.location = downloadUrl;
                  }
  
                  setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup
              }
          } else {
              alert("file is not available");
          }
      },
      error:function(){
          alert("error occure here...");
      }
  });
  </code></pre>