This question already has an answer here:
- Binary files corrupted - How to Download Binary Files with AngularJS 2 answers
i have a service function which return me 3 result files(2 text files, 1 zip file).
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.
my controller resolved the promise from my service. if i download a text file -> the file says [object object] ..
i have seen in my browser that the response body is from content-type: text/plain
i am bit confused :/
controller :
$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);
})
};
service :
.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;
view:
<span ng-show="dataTable">
<table>
<tr ng-repeat="r in results">
<td><a href="" ng-click="getResult(r.id)" download>{{r.name}}</a></td>
</tr>
</table>
</span>
Update:
this is my service where i set the responsetype to arraybuffer
.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;
this is my call in controller :
$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);
})
};
}])
but if i download the zip file, its still corrupt and a error while extract the zip -.-