简体   繁体   中英

angularjs download corrupted zip file

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

尝试在Github中的FileSaver上检查此错误: 保存Zip文件问题

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM