upload file to RESTful service in angularjs upload file to RESTful service in angularjs angularjs angularjs

upload file to RESTful service in angularjs


Something like this should work well to send as multipart/form-data request to backend API:

var file = ... // get from file input;var backendUrl = ...var fd = new FormData();fd.append('myFile', file, 'filename.ext');$http.post(backendUrl, fd, {    // this cancels AngularJS normal serialization of request    transformRequest: angular.identity,    // this lets browser set `Content-Type: multipart/form-data`     // header and proper data boundary    headers: {'Content-Type': undefined}}).success(function(){    //file was uploaded}).error(function(){    //something went wrong });

See here for reference:

FormData API Doc

Using FormData Objects (MDN)

multipart-formdata file upload with AngularJS


You can upload file using ngFileUpload or angularFileUpload directives.

In case of angularFileUpload, you use .upload in controller and in case of ngFileUpload, you use .http

Also, you can also use application/x-www-form-urlencoded content type instead of multipart provided your file is not huge. In case of application/x-www-form-urlencoded, you can just receive the value in rest webservice as normal InputStream thereby requiring no need of marshalling of multipart data.

You may refer the below for the possible ways to upload file using angular js and rest web service:

http://technoguider.com/2015/08/file-upload-using-angular-js-and-rest-web-service/