title: Downloading a file using AJAX GET from Spring Service
body: <p>I'm trying to implement a Service that automatically starts a download with the requested file.</p>
  
  <p>This is my AJAX call:</p>
  
  <pre><code>function downloadFile(fileName) {
    $.ajax({
      url : SERVICE_URI + "files/" + fileName,
      contentType : 'application/json',
      type : 'GET',
      success : function (data)
      {
        alert("done!");
      },
      error: function (error) {
        console.log(error);
      }
    });
  }
  </code></pre>
  
  <p>and this is my Spring Service method GET:</p>
  
  <pre><code>@RequestMapping(value = "/files/{file_name}", method = RequestMethod.GET)
  public void getFile(@PathVariable("file_name") String fileName,
                      HttpServletResponse response) {
      try {
          // get your file as InputStream
          FileInputStream fis = new FileInputStream( fileName + ".csv" );
          InputStream is = fis;
          // copy it to response's OutputStream
          ByteStreams.copy(is, response.getOutputStream());
          response.setContentType("text/csv");
          response.flushBuffer();
      } catch (IOException ex) {
          throw new RuntimeException("IOError writing file to output stream");
      }
  
  }
  </code></pre>
  
  <p>When my client requests the existing file from the server, the AJAX <code>success()</code> method is executed but the file is not even downloading. Am I doing anything wrong?</p>