0

I'm using AngularJS to communicate with a RESTful web service (running with spring boot), using $resource. I'm trying to upload some files and send form fields within a same multipart post request but i am getting the following error:

"Unsupported Media Type" exception: "org.springframework.web.HttpMediaTypeNotSupportedException" message: "Content type 'application/xml' not supported" path: "/study/upload" status: 415

here is my view form :

<form method="post" enctype="multipart/form-data" ng-submit="submit()">
  <table>
    <tr><td>File to upload:</td><td><input type="file" name="file" ng-model="form.file"/></td></tr>
    <tr><td>Name:</td><td><input type="text" name="name" ng-model="form.name"/></td></tr>
    <tr><td></td><td><input type="submit" value="Upload"  /></td></tr>
  </table>
</form>

here is the view controller:

$scope.submit=function(){
    GalleryService.upload({file: $scope.form.file, name: $scope.form.name}, function(data) {
         console.log("Success ... " + status);
    }, function(error) {
         console.log(error);
    });
}

gallery.service :

(function() {

'use strict';

angular .module('ekellsApp') .factory('GalleryService', GalleryService);

function GalleryService($resource, restApi) {

return $resource(restApi.url + '/study/:action', {},{
  save: {method: 'POST', params: {action: 'save'}},
  getAll: {method: 'GET', params: {action: 'getAll'},isArray:true},
  upload: {method: 'POST', params: {action: 'upload'}, transformRequest: angular.identity,headers: { 'Content-Type': undefined }}


});

}

})();

and here is the REST controller:

@RequestMapping(value = "/upload",method = RequestMethod.POST,headers = "content-type=multipart/*")
public String handleFileUpload(@RequestParam("name") String name,
                               @RequestParam("file") MultipartFile file,
                               RedirectAttributes redirectAttributes) {
    if (name.contains("/")) {
        redirectAttributes.addFlashAttribute("message", "Folder separators not allowed");
        return "redirect:upload";
    }
    if (name.contains("/")) {
        redirectAttributes.addFlashAttribute("message", "Relative pathnames not allowed");
        return "redirect:upload";
    }

    if (!file.isEmpty()) {
        try {
            BufferedOutputStream stream = new BufferedOutputStream(
                    new FileOutputStream(new File("user/bouhuila/" + name)));
            FileCopyUtils.copy(file.getInputStream(), stream);
            stream.close();
            redirectAttributes.addFlashAttribute("message",
                    "You successfully uploaded " + name + "!");
        }
        catch (Exception e) {
            redirectAttributes.addFlashAttribute("message",
                    "You failed to upload " + name + " => " + e.getMessage());
        }
    }
    else {
        redirectAttributes.addFlashAttribute("message",
                "You failed to upload " + name + " because the file was empty");
    }

    return "redirect:upload";
}

1 Answer 1

0

I cannot see what

 GalleryService.upload

is doing, but it looks like you are not really sending the form to the server, but only some fields. In this case

enctype="multipart/form-data"

would not be used, that could explain the error you are getting.

Have a look in the developer tools in your browser, which request is actually sent to your server and which content type it is using..

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.