3

I am trying to use an angular resource to get a binary file. I am able to do this with an http.get.

return $http.get("http://localhost:8080/users/my_user/avatar", {responseType:'arraybuffer'});

This returns a string of the file. That is what I want.

When trying to use the resource:

var resource4 = $resource('/users/:userId/avatar',{},{'get':{method:'GET',cache:false,responseType:'arraybuffer'},'getCached':{method:'GET',cache:true,responseType:'arraybuffer'}, 'postList':{method:'POST', isArray:true}});

I return an object with a get method here:

getAvatar: function(userId, successCallback, errorCallback) {

                var requestData = { userId: convertValueForRest(userId)};
                return resource4.get(requestData, successCallback, errorCallback);

            }

Then I inject it into a service and pass it through:

function getAvatar(user){
    return UserDetailsInterface.getAvatar(user);
}

I grab the data from $promise here:

UserPreferences.getAvatar(userName).$promise
                    .then(function success(image){

What is returned is the file's characters split out into a huge array. I am really not sure why.

2
  • Try setting isArray: false in the get config Commented Jan 25, 2016 at 19:02
  • That does not seem to work. I thought that isArray defaulted to false if omitted. Commented Jan 25, 2016 at 21:59

1 Answer 1

10

I figured out the problem. I needed to add transformResponse to handle the arraybuffer being returned in the resource.

transformResponse: function(data, headersGetter) { return { data : data }}

This answer was what I needed: https://stackoverflow.com/a/24290186/5838629

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.