Reputation: 1646
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
Upvotes: 0
Views: 1439
Reputation: 1083
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.
Upvotes: 0
Reputation: 1413
Try check this bug on FileSaver in Github: saving Zip file problems
Upvotes: 2