I implemented download with angular which worked OK for txt files, but when
I started testing it with other binary formats (PNG for instance), I notice
the files are being downloaded from server with bad content. Here is my
code.
app.controller('DownloadCtroller', function($scope, $http) {
$scope.downloadFile = function () {
if (angular.isUndefined($scope.getfile)){
alert('File name is missing')
return;
}
$http({ method: 'GET', url: 'get?filename='+$scope.getfile
, headers: {
"Accept":
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
}
})
.then(function (response) {
var anchor = angular.element('<a/>');
anchor.attr({
href: 'data:attachment;charset=utf-8,'+
encodeURI(response.data),
target: '_blank',
download: $scope.getfile
})[0].click();
}, function errorCallback (response) {
if(response.status == 400) alert('File name is missing');
else if(response.status == 404) alert('File not found on server');
else alert('Server error. Try again later');
}
);
}});
I also tried to replace 'encodeURI(response.data)' with just
'response.data', but that didn't help either.
--
You received this message because you are subscribed to the Google Groups
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.