title: How to download file from server using jQuery AJAX and Spring MVC 3
body: <p>I want to implement downloading (with AJAX) of uploaded file from server. On the server side I wrote the code</p>
  
  <pre><code>@RequestMapping(value = "/getInvoice/approvalId/{approvalId}", method = RequestMethod.GET)
  public
  @ResponseBody
  byte[] getInvoice(@PathVariable("approvalId") Integer approvalId, HttpServletResponse response) throws IOException {
      String fileName = this.approvalService.getFullInvoicePath(approvalId);
      File file = new File(fileName);
  
      response.setContentType("application/octet-stream");
      response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
      response.setHeader("Pragma", "no-cache");
      response.setHeader("Cache-Control", "no-cache");
      response.setContentLength((int) file.length());
      return FileUtils.readFileToByteArray(file);
  }
  </code></pre>
  
  <p>Fiddler2 shows response:</p>
  
  <pre><code>HTTP/1.1 200 OK
  Server: Apache-Coyote/1.1
  Content-Disposition: attachment; filename="invoice.pdf"
  Pragma: no-cache
  Cache-Control: no-cache
  Content-Type: application/octet-stream;charset=UTF-8
  Content-Length: 1028351
  Date: Sun, 17 Jul 2011 08:16:41 GMT
  
  %PDF-1.4
  %����
  6 0 obj &lt;&lt;/Linearized 1/L 1028351/O 8/E 1024254/N 1/T 1028185/H [ 5056 544]&gt;&gt;
  endobj
  
  xref
  6 238 
  *** FIDDLER: RawDisplay truncated at 128 characters. Right-click to disable truncation. ***
  </code></pre>
  
  <p>How to handle and force browser to download file using jQuery?</p>