title: How to download a zip file in AngularJS / FilesaverJS
body: <p>i have a service function which return me 3 result files(2 text files, 1 zip file).</p>
  
  <p>i want to check the header of the response to see if the file is a zip or a text to set the right configuration to download the file.</p>
  
  <p>my controller resolved the promise from my service. if i download a text file -> the file says [object object] ..</p>
  
  <p>i have seen in my browser that the response body is from content-type: text/plain</p>
  
  <p>i am bit confused :/</p>
  
  <p>controller :</p>
  
  <pre><code>$scope.getResult = function(rid) {
          console.log($scope.id);
          GetResultFile.getResult($scope.id, rid)
          .then(function (data, headers) {
              //console.log(headers(Content-type));
              console.log("Download erfolgreich");
              console.log(data.status);
              var file = new Blob([data], {type: 'text/plain;charset=utf-8'});
              FileSaver.saveAs(file, 'test.txt');
  
          }, function (data) {
              console.log("Download ERROR!");
              console.log(data.status);
          })
      };      
  </code></pre>
  
  <p>service :</p>
  
  <pre><code>.factory('GetResultFile',
  ['$http', '$q',
  function ($http, $q) {
  
      var service = {};
      service.getResult = function(id, rid) {
  
          var deferred = $q.defer();
  
          $http
          .get('http://localhost:9999/v1/jmeter/' + id + '/results/' + rid, {cache: false})
          .then(function(data, status, headers, config) {
              if(data.status == 200) {
                  console.log(data.status);
                  deferred.resolve(data);
              }
              else {
                  deferred.reject(data);
              }
          });
          return deferred.promise;                
      }
      return service;
  </code></pre>
  
  <p>view:</p>
  
  <pre><code>&lt;span ng-show="dataTable"&gt;
      &lt;table&gt;
          &lt;tr ng-repeat="r in results"&gt;
              &lt;td&gt;&lt;a href="" ng-click="getResult(r.id)" download&gt;{{r.name}}&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/span&gt;
  </code></pre>
  
  <p>Update:</p>
  
  <p>this is my service where i set the responsetype to arraybuffer</p>
  
  <pre><code>.factory('GetZip',
  ['$http', '$q',
  function ($http, $q) {
  
      var service = {};
      service.getZip = function(id, rId) {
  
          var deferred = $q.defer();
  
          $http
          .get('http://localhost:9999/v1/jmeter/' + id + '/results/' + rId, {cache: false, responseType: 'arraybuffer'})
          .then(function(response) {
              if(response.status == 200) {
                  console.log(response.status);
                  deferred.resolve(response);
              }
              else {
                  deferred.reject(response);
              }
          });
          return deferred.promise;                
      }
      return service;
  </code></pre>
  
  <p>this is my call in controller :</p>
  
  <pre><code>$scope.getResult = function(rId, rName) {
          console.log($scope.id);
          console.log(rName);
          GetResultFile.getResult($scope.id, rId)
          .then(function(response) {
              var data = "";
              var headerType = "";
              data = response.data;
              headerType = response.headers('content-type');
              console.log(response.headers('content-type'));
              console.log("Download erfolgreich");
              console.log(response);
              // zip data need to be handle with a binary stream
              if(headerType == 'application/zip;charset=UTF-8') {
                  GetZip.getZip($scope.id, rId)
                  .then(function(response) {
                      var file = new Blob([response.data], {type: "application/octet-stream"});
                      FileSaver.saveAs(file, rName);
                      console.log("zipper");
                  }, function(response) {
                      console.log("Download ERROR");
                      console.log(response.status);
                  })
              } else {
                  var file = new Blob([data], {type: headerType});
                  FileSaver.saveAs(file, rName);
                  console.log("else");
              }
          }, function(response) {
              console.log("Download ERROR!");
              console.log(response.status);
          })
      };              
  </code></pre>
  
  <p>}])</p>
  
  <p>but if i download the zip file, its still corrupt and a error while extract the zip -.-</p>