0

in my angular application I need to download a zip file, the download starts regularly, but the client download a corrupted zip file, whose size is only 15 byte. this is the code for the request:

$http.post('/api/download/',data, {
          dataType : "binary",
          processData : false,
          accept:'application/zip',
          Encoding: 'gzip',
          responseType:'arraybuffer'})

this is how I handle the successful response:

function() {
   var data = {}
   data.files = $scope.selectedFiles
   FileService.download(data,function(resp){
       var blob = new Blob([resp], {type: "application/octet-stream"})
       console.log('response',resp)
       FileSaver.saveAs(blob,'registrazioni.zip',(err) => {
                              console.log('saved success,error',err);
       })                     
   })
}

I read many similar questions, but theirs solutions do not apply to me

Pradeepb
  • 2,564
  • 3
  • 25
  • 46
arpho
  • 1,576
  • 10
  • 37
  • 57
  • There is a [similar question here](http://stackoverflow.com/questions/42517619/angularjs-save-image-file-sent-from-web-api-2/42517789#42517789) that might be useful to you. – lealceldeiro Mar 13 '17 at 18:33

2 Answers2

2

Try check this bug on FileSaver in Github: saving Zip file problems

Matej Marconak
  • 1,365
  • 3
  • 20
  • 25
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – morten.c Mar 14 '17 at 00:33
0

First of all you need to set the responseType to arraybuffer.See sending_and_Receiving_Binary_Data. So your code will like this:

$http.post('/postUrlHere',{myParams}, {responseType:'arraybuffer'})
  .success(function (response) {
       var file = new Blob([response], {type: 'application/pdf'});
       var fileURL = URL.createObjectURL(file);
});

In Controller:

$scope.cont = $sce.trustAsResourceUrl(fileURL);

In html Page:

<embed ng-src="{{cont}}" style="width:100px;height:100px;"></embed>

This code Will be help you to solve problem.

Feras Al Sous
  • 1,073
  • 1
  • 12
  • 23