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

2
  • 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. Commented Jan 23, 2014 at 9:12
  • possible duplicate of downloading files from nodejs using angular at client side Commented Dec 24, 2014 at 8:17

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.