title: Get excel file (.xlsx) from server response in ajax
body: <p>I have got a problem with getting excel file and opening download window in the browser after getting a response (in success ajax method) with that file. I have got appropriate <code>Content-Type and Content-Disposition headers</code>, I tried using <strong>Blob</strong> in js and I couldn't achieve what I want - simple file downloading.<br>
  I accomplished few versions of my ajax, one of them is below. I developed ajax which returns excel file which I couldn't open properly because it's corrupted (despite .xlsx extension).   </p>
  
  <p>Maybe the problem is with inappropriate data type used in Blob constructor? </p>
  
  <p>I tried using "xhr.response" instead of "data" from success method arguments but it doesn't work too. I checked Response Headers in Developer Tools in Chrome and they are set properly.<br>
  The important thing - all the excel workbooks created on the server side is correct because it worked in the previous version when data was sent in URL, not in ajax post.</p>
  
  <p>Controller method in Java/Spring server side below:</p>
  
  <pre><code>response.reset();
  response.setContentType("application/vnd.ms-excel");
  response.addHeader("Content-Disposition","attachment;filename=\"" + className + " " +  title + ".xlsx\"");
      try (ServletOutputStream output = response.getOutputStream()){
          workbook.write(output);
          output.flush();
      } catch (Exception e) {
          throw new RuntimeException(e);
      }
  </code></pre>
  
  <p>My Ajax to download file and open download window:</p>
  
  <pre><code>$.ajax({
      url: myUrl,
      type: 'POST',
      data: myData,
      success: function(data, status, xhr) {
          var contentType = 'application/vnd.ms-excel';
  
          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, '');
          }
          console.log("FILENAME: " + filename);
  
          try {
              var blob = new Blob([data], { type: contentType });
  
              var downloadUrl = URL.createObjectURL(blob);
              var a = document.createElement("a");
              a.href = downloadUrl;
              a.download = filename;
              document.body.appendChild(a);
              a.click();
  
          } catch (exc) {
              console.log("Save Blob method failed with the following exception.");
              console.log(exc);
          }
  </code></pre>