Downloading Blob via http using AngularJS and Angular-FileSaver.js -


this question exact duplicate of:

i'm having really hard time solving problem. i've tried many different solutions i'm not sure need fix now. i'm attempting retrieve blob via http , download file user using filesaver.js reason, every time attempt open image "damaged, corrupted, or large" message. i've attempted playing 'responsetype' (changing 'blob'), adding 'accept' header. nothing seems work me!!

can maybe point me in right direction?

service

download: function(blobid, token) {   var req = {     method: 'get',     cache: false,     url: 'api/blob/downloadblob/' + blobid,     headers: {      'responsetype': 'arraybuffer',      'authorization': token     }   };    return $http(req)     .then(function (response) {       return response;     }, function(response) {       // went wrong       return $q.reject(response.data);   }); } 

controller

$scope.downloadfile = function () {   data.download($scope.blobid, $scope.token).then(function (response) {     var blob = new blob([response], { type: 'image/png' });     filesaver.saveas(blob, 'download.png');   }); }; 

first 2 problems can see are...

  1. the responsetype config property should not in headers object
  2. you passing response object blob constructor want pass response.data.

i'd go with

return $http.get('api/blob/downloadblob/' + blobid, {   responsetype: 'blob',   headers: {    authorization: token   },   transformresponse: function(data) {     return data // there's no need attempt transformations   } }).then(function(response) {   return response.data // provider consumers need blob data }) 

and in consumer...

data.download($scope.blobid, $scope.token).then(function(blob) {   filesaver.saveas(blob, 'download.png') }) 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -