title: download a file using Angular JS and a Spring-based RESTful web service
body: <p>I'm using Spring boot and Angular JS  .  I have a Spring REST Controller that I use to download a File. when I call it using <a href="http://localhost:8080/download" rel="noreferrer">http://localhost:8080/download</a> it works and the file is downloaded.  Now I have a button ,when I click on it , I  the file will be downloaded . so I wrote a function in my angular js controller to get the url of my spring web service  but when I tested it nothing happened . <strong>what should I do to fix this ? Is there a better way to download files using Spring and Angular ?</strong></p>
  
  <pre><code> /**
   * Size of a byte buffer to read/write file
   */
  private static final int BUFFER_SIZE = 4096;
  private String filePath = "C:\\Users\\marwa\\Desktop\\marwa\\gg.jpg";
  
  /**
   * Method for handling file download request from client
   */
  @RequestMapping (value="/download", method=RequestMethod.GET )
  public void doDownload(HttpServletRequest request,
          HttpServletResponse response) throws IOException {
  
      // get absolute path of the application
      ServletContext context = request.getServletContext();
      String appPath = context.getRealPath("");
      System.out.println("filepath = " + filePath);
  
      // construct the complete absolute path of the file
  
      File downloadFile = new File(filePath);
      FileInputStream inputStream = new FileInputStream(downloadFile);
  
      // get MIME type of the file
      String mimeType = context.getMimeType(filePath);
      if (mimeType == null) {
          // set to binary type if MIME mapping not found
          mimeType = "application/octet-stream";
      }
      System.out.println("MIME type: " + mimeType);
  
      // set content attributes for the response
      response.setContentType(mimeType);
      response.setContentLength((int) downloadFile.length());
  
      // set headers for the response
      String headerKey = "Content-Disposition";
      String headerValue = String.format("attachment; filename=\"%s\"",
              downloadFile.getName());
      response.setHeader(headerKey, headerValue);
  
      // get output stream of the response
      OutputStream outStream = response.getOutputStream();
  
      byte[] buffer = new byte[BUFFER_SIZE];
      int bytesRead = -1;
  
      // write bytes read from the input stream into the output stream
      while ((bytesRead = inputStream.read(buffer)) != -1) {
          outStream.write(buffer, 0, bytesRead);
      }
  
      inputStream.close();
      outStream.close();
  
  }
  </code></pre>
  
  <p>my angular js function ( I added console.log("ok") to see if I get a result from the spring controller and it printed ok )</p>
  
  <pre><code>$scope.downloadFile = function () {                   
          $http({method: 'GET', url: '/download'}).
           success(function(result) {
                          console.log("ok");
            }).
       error(function(data, status, headers, config) {
              console.log("oops");
           });
   };
  </code></pre>
  
  <p>and my button </p>
  
  <pre><code>&lt;button  class="btn btn-success" ng-click="downloadFile()"&gt;download&lt;/button&gt;
  </code></pre>