Exception when sending Multipart file from controller AngularJS on server Spring Boot

0

I'm having trouble sending client side files (images, pdfs) to the server side of my app.

Whenever I try to send a file, Spring gives me this exception:

status:"Bad Request"
exception:"org.springframework.web.multipart.support.MissingServletRequestPartException"
message:"Required request part 'file' is not present"
path:"/upload"
status:400

The Spring controller looks like this:

@RestController
@RequestMapping("/upload")
public class UploadController {

    @RequestMapping(value = "", 
                    method = RequestMethod.POST)
    public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile file) {

        try {
            if(file != null) {
                System.out.println("Arqiuivo recebido!");
            }
        }

        catch (Exception e) {
            System.out.println("Deu erro!");
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }

        return new ResponseEntity<>(HttpStatus.OK);
    } 
}

And I also configured Multipart to solve like this:

 @Bean
 public MultipartResolver multipartResolver() {
     return new CommonsMultipartResolver();
 }

On the client side, as I'm using the ng-file-upload module, I'm using the following div that appears in the developer's github:

<div class="button" ngf-select ng-model="imagem" name="file" ngf-pattern="'image/*'"
  ngf-accept="'image/*'" ngf-max-size="20MB" ngf-min-height="100" 
  ngf-resize="{width: 100, height: 100}">Select
</div>

And in the AngularJS controller, I have the following:

Upload.upload({
    url: 'upload',
    data: {file: $scope.imagem}
}).progress(function (evt) {

}).success(function (data, status, headers, config) {

});

So far I have not been successful. Could someone help me?

EDIT

I tried using Postman to send a pro server image, but it gives the same exception as when I try to send it to my client:

    
asked by anonymous 12.10.2016 / 20:17

1 answer

0

It seems all right with your java code, maybe it's something on your client, have you ever tried to send the file by curl or postman?

    
13.10.2016 / 13:28