title: How to let user download a file in ajax success method?
body: <p>I'm trying to let user download a file from server.
  I use ServletOutputStream in my controller (here is the code)</p>
  
  <pre><code>@RequestMapping(value = "/get-backup-file", method = RequestMethod.GET)
  @ResponseBody
  public void getBackupFile(
      HttpServletRequest request, 
      HttpServletResponse response) throws MalformedURLException, IOException {
  
      File backupFile = new File("PATH_TO_FILE");        
  
      ServletOutputStream out = response.getOutputStream();
  
      response.setContentType("application/octet-stream");
      response.setContentLength((int)backupFile.length());
      response.setHeader("Content-Disposition", "attachment; filename=\"" + "database backup" + "\"");
  
      FileInputStream in = new FileInputStream(backupFile);
      byte[] buffer = new byte[4096];
  
      int length;
      while( (length = in.read(buffer) ) &gt; 0) {
          out.write(buffer, 0, length);
      }
      in.close();
      out.flush();        
  } 
  </code></pre>
  
  <p>My client side looks like this: </p>
  
  <pre><code>      $.ajax({
          url: 'URL_TOCONTROLLER_METHOD',
          contentType: "application/octet-stream; charset=utf-8",
          type: 'GET',
          success: function(data) {
              console.log(data);
          },
          error: function(data) {   
              console.log("error");
          }
      });
  </code></pre>
  
  <p>when I console.log the data it has the content of the file, but I want this file to be downloaded tu the user, not just printed. how can make let user to save the data as file?</p>