title: Download a file with AJAX and Spring Boot
body: <p>I'd like to download a file async. I have a working sync method that runs when called from a url:</p>
  
  <pre><code>&lt;a th:href="@{'download/' + ${person.number}}"&gt;download&lt;/a&gt;
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>@RequestMapping(value = "/download/{number}", method = RequestMethod.GET)
  public ResponseEntity&lt;byte[]&gt; downloadFile(@PathVariable Long number) throws Exception {
  
   byte[] array = Files.readAllBytes(file.toPath());
   String fileName = "attachment; filename="+number+".xls";
  
   HttpHeaders responseHeaders = new HttpHeaders();
   responseHeaders.set("charset", "utf-8");
   responseHeaders.setContentType(MediaType.valueOf("text/html"));
   responseHeaders.setContentLength(array.length);
   responseHeaders.set("Content-disposition", fileName);
  
   return new ResponseEntity&lt;byte[]&gt;(array, responseHeaders, HttpStatus.OK);
  }
  </code></pre>
  
  <p>Now I'd like to call it via jQuery/AJAX, so what I have is:</p>
  
  <pre><code>&lt;button class="btn" id="downloadButton" name="23"&gt;download&lt;/button&gt;
  
  $('#downloadButton').click(function() {
  
      var urlToCall = "/download/"+this.name;
  
      $.ajax({
          type:'GET',
          url: urlToCall,
          success: function(data){
  
          }
      });
  });
  </code></pre>
  
  <p>This code calls my Java method and then JS success function, but a file is not 
  downloaded. I tried with @ResponseBody but it didn't change anything. What did I miss?</p>