9

I'm using AngularJS to interact with a RESTful webservice, using Restangular to abstract the various entities exposed. Some of this entities are images or files, so I need to be able to use the post action of Restangular to send both binary data and text fields within the same request.

How can I use Restangular to send data with uploaded images or files, to a restful webservice in a single POST request?

Hegler
  • 101
  • 1
  • 4
  • To send a file through an ajax post request you need to make a multipart form submit. This answer might help you get started: http://stackoverflow.com/questions/6974684/how-to-send-formdata-objects-with-ajax-requests-in-jquery According to this, you basically are submitting a form, the tricky part will be to ensure $http is not setting the wrong content type. Take a look at Restangular withHttpConfig option https://github.com/mgonto/restangular#using-local-http-configuration, or maybe setting the Content-Type header directly with a customPost – guzart Nov 05 '14 at 17:20

1 Answers1

2

I just answered a very similar question here which explains how to use a file-model directive and creating a formData object which then is posted to an api.

Using Restangular instead of $http, the only part that's different is of course how the post is done.

Here is how the post is done using restangular:

Restangular.one('api/url/to/endpoint')
.withHttpConfig({transformRequest: angular.identity})
.customPOST(fd, '', undefined, {'Content-Type': undefined})
Community
  • 1
  • 1
Erik Svedin
  • 1,286
  • 12
  • 26