1

I wanted to pass multipart/formdata through a $resource in angular in order to post an image. I followed the magic answer founded here AngularJS: Upload files using $resource (solution)

But this solution apply for all requests in my modules. And I want it to apply only for one $resource or two. If possible I want to keep JSON for the others requests. In fact I'm using nested properties and I would like to keep it.

Is here any way to do it ?

Thanks

Community
  • 1
  • 1
cocoggu
  • 401
  • 7
  • 18

1 Answers1

3

As per $request documentation, the transformRequest() can be applied per method; you will have to redefine the method though. Some sample code would be:

var MyResource = $resource("url", {
    myPost: {
        method: "POST",
        transformRequest: function(data) {
            // code from http://stackoverflow.com/questions/21115771/angularjs-upload-files-using-resource-solution
        }
    }
);

This is the barebone setup, you will probably need some adjustments. It would be used as (again the simplest case):

MyResource.myPost(formData); // formData contains properties and the image(s)
Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90