title: File download/save from a Jersey rest api call using ANGULARJS . file is attached with response as "Content-Disposition"
body: <p>I am new to angular ..  I hava java rest api which return CSV file in response as attachment  as | "Content-Disposition", "attachment; filename=" |   content-type :application/octet-stream</p>
  
  <p>Now when i am calling this api from AngularJS using $http i am getting response.data ="" (blank)</p>
  
  <p>I am using basic authorisation for security so I have to pass Header while calling calling API so can't use link click or new window open fro CSV download.</p>
  
  <p>to test when i removed authorisation and hit the url in browser then CSV file is being downloaded.so no issue at server side .</p>
  
  <p>I need help at angularjs side to download CSV file from web api response as attachment. </p>
  
  <p>Here is my Java API Code</p>
  
  <pre><code>public class ServiceAPI {
  
  @GET
  @Produces(MediaType.APPLICATION_OCTET_STREAM)
  public Response getFileAsCSVFile(){
  
  byte[] file=null;
      try {
          ArrayList&lt;&gt; List=new ArrayList&lt;&gt;();// data retrieved from DB 
  
          if(null != List){
               file=convertJsonToCSV(new Gson().toJson(List));
  
          }
  
      } catch (ParseException e) {
  
  
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
  
      return            Response.ok(getBytes(file),MediaType.APPLICATION_OCTET_STREAM).header("Content-Disposition", "attachment; filename=" + "FileName.csv").build();
   }
   }
  </code></pre>
  
  <p>and Angular code :</p>
  
  <pre><code>app.controller('review', ['$scope', '$http',  function ($scope, $http){
  
  
  
  $scope.fromDate = new Date();
  $scope.toDate = new Date();
  
  
  $scope.minDate = new Date(
      $scope.fromDate.getFullYear(),
      $scope.fromDate.getMonth() - 2,
      $scope.fromDate.getDate(),
  
      $scope.toDate.getFullYear(),
      $scope.toDate.getMonth() - 2,
      $scope.toDate.getDate()
  );
  
  $scope.maxDate = new Date(
      $scope.fromDate.getFullYear(),
      $scope.fromDate.getMonth() - 2,
      $scope.fromDate.getDate(),
  
      $scope.toDate.getFullYear(),
      $scope.toDate.getMonth() - 2,
      $scope.toDate.getDate()
  );
  
  $scope.reviews = json;
  
  
  function openSaveAsDialog(filename, content, mediaType) {
      var blob = new Blob([content], {type: mediaType});
      saveAs(blob, filename);
  }
  
  function callApi(url) {
  
     // var dat=apiFactory.getServiceData(url);
     // console.log(dat);
     // apiFactory.getServiceData(url);
  
      var responseType='arraybuffer';
      var expectedMediaType='application/octet-stream';
      $http.get(url, {
  
          headers: {
  
              accept: expectedMediaType
          },
          responseType:responseType,
          cache: true,
          transformResponse: function (data) {
              var pdf;
              if (data) {
                  pdf = new Blob([data], {
                      type: expectedMediaType
                  });
              }
              return {
                  response: pdf
              };
          }
      }).then(function (data,status,headers) {
          var filename='Preview.csv',
              octetStreamMime = "application/octet-stream",
              contentType;
  
          headers = data.headers();
          contentType = headers["content-type"] || octetStreamMime;
  
  
       //   openSaveAsDialog(filename, response.data, expectedMediaType);
          if (navigator.msSaveBlob) {
              var blob = new Blob([data], { type: contentType });
              navigator.msSaveBlob(blob, filename);
          } else {
              var urlCreator = window.URL || window.webkitURL || window.mozURL || window.msURL;
  
              if (urlCreator) {
                  // Try to use a download link
                  var link = document.createElement("a");
  
                  if ("download" in link) {
                      // Prepare a blob URL
                      var blob = new Blob([data.data], { type: contentType });
                      var url = urlCreator.createObjectURL(blob);
  
                      link.setAttribute("href", url);
                      link.setAttribute("download", filename);
  
                      // Simulate clicking the download link
                      var event = document.createEvent('MouseEvents');
                      event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
                      link.dispatchEvent(event);
                  } else {
                      // Prepare a blob URL
                      // Use application/octet-stream when using window.location to force download
                      var blob = new Blob([data], { type: octetStreamMime });
                      var url = urlCreator.createObjectURL(blob);
                      $window.location = url;
                  }
              }
          }
      });
  
  };
      $scope.submit = function (fromDate, toDate) {
  
  
         $scope.url = API_url;
  
  
          var resp =callApi(($scope.url).split(" ").join("%20"));
  
          console.log(resp);
      };
  },
  ]);
  </code></pre>