0

I need to download a zip file by making post request. Fraom backend they are zipping the file and sending.

Backend Values they set

Content-Disposition →attachment;
filename="sample.zip"
Content-Type →application/zip 

UI Code:

downloadMethod = function (path, item) {
      var promise = $http({ 
          method: "POST",
          data: item,
          url: API_SERVER.url+path,
          headers: { 'Content-Type': 'application/zip; charset=UTF-8'},
          responseType: 'arraybuffer'
      });
      promise.success(function(response, status, headers, conf) { 
        return response;
      }).error(function(response){
        return response;
      });
      return promise;
  };

On Success i am saving the file using Filesaver

var blob = new Blob([data)], {type:"application/zip"});
            saveAs(blob, "sample.zip");

The file is downloading. But while extracting the file I am getting the error

ERROR: CENTRAL DIRECTORY NOT FOUND

and also m getting File cannot be opened or it is not a valid zip file.

But when i try to hit the api from postman it is download and i can able to extract the file.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Sunny
  • 1,167
  • 5
  • 15
  • 27

1 Answers1

0

Replace the .success and .error methods with .then and .catch methods:

downloadMethod = function (path, item) {
      var promise = $http({ 
          method: "POST",
          data: item,
          url: API_SERVER.url+path,
          headers: { 'Content-Type': 'application/zip; charset=UTF-8'},
          responseType: 'arraybuffer'
      });
      promise.then(function(response) { 
        ̶r̶e̶t̶u̶r̶n̶ ̶r̶e̶s̶p̶o̶n̶s̶e̶;̶
        return response.data;
      }).catch(function(response){
        ̶r̶e̶t̶u̶r̶n̶ ̶r̶e̶s̶p̶o̶n̶s̶e̶;̶
        throw response;
      });
      return promise;
  };

The deprecated .success and .error methods ignore return statements.

Also notice that the signatures are difference.

For more information see

georgeawg
  • 48,608
  • 13
  • 72
  • 95