4

I'm trying to use angularJs resource to download a file from a server but it dosen't work. my code is as following:

// service
angular.module("someModule")
.factory('generate', ['$resource', '$rootScope', function ($resource, $rootScope) {
return $resource('some url...');}]);


//Js controller file - I'm getting the 'generate' service in the head of the controller
 $scope.$on('generateFile', function(){
    generate.get();
});


//html
<a ng-click="$emit('generateFile')"></a>

When i'm typing the url it download the file - so the server side is fine. However, I couldn't find any example of using angular resource to download a file. Thanks for the help

Rivi
  • 672
  • 13
  • 22
  • Can you post the content of that file as an example? Using JSON with $resource is quite straightforward, but using XML seems not that easy. You might be better of using $http, and parse the data manually. – martinczerwi Jan 23 '14 at 09:12
  • possible duplicate of [downloading files from nodejs using angular at client side](http://stackoverflow.com/questions/24652927/downloading-files-from-nodejs-using-angular-at-client-side) – Paul Sweatte Dec 24 '14 at 08:17

1 Answers1

0

Use an interceptor to grab the responseXML:

function grabResponseXML($q, dependency1, dependency2) 
    {
    return {
           'response': function(response) 
              {
              var deferred = $q.defer();

              if (response.headers()['content-type'] === 'text/xml; charset="UTF-8"') 
                {
                localStorage.setItem('xmlResponse', response.responseXML);
                }
              deferred.resolve(response);

              return deferred.promise;
              }
           }
    }             

$httpProvider.interceptors.push(grabResponseXML);

References

Paul Sweatte
  • 22,871
  • 7
  • 116
  • 244