0

I've been looking at this but it seems my problem is elsewhere. I am trying to upload a file. The input is currently defined as:

<input 
    type="file" 
    style="display: none;"
    name="file"     
    multiple    
    nv-file-select                  
    uploader="uploader">

This is how the upload is performed:

var uploader = $scope.uploader = new FileUploader({
    url: 'http://localhost:8080/rest-api/dl4j/we/uploadModel'
});

uploader.onAfterAddingFile = function($modelFile) {

    var fd = new FormData();        
    fd.append('file', $modelFile.file);

    $http.post($modelFile.url, fd, {
        headers: {
            'Content-Type': undefined
        },
        transformRequest: angular.identity          
    })
    .then(
        function (data) {
            alert("upload success");
        }, 
        function (data, status) {
            alert("upload error");
        }
     );
};

Whereas this is the Spring REST endpoint:

@PostMapping(WordEmbeddingApiPaths.UPLOAD_MODEL)
@RequestMapping(method=RequestMethod.POST, headers={"Content-Type=multipart/form-data"})
public ResponseEntity<WordVectorListDto> uploadModel( 
        @RequestParam("file") MultipartFile file,
        RedirectAttributes redirectAttributes) {

    LOGGER.debug("POST uploadModel");

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

The problem is though, that an exception is getting thrown by Spring, telling me that the parameter file is not present:

org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present

This is the request information:

enter image description here

How can I make this file upload work?

Community
  • 1
  • 1
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378

1 Answers1

1

I guess it can be related to the content type In your code i see this:

$http.post($modelFile.url, fd, {
        headers: {
            'Content-Type': undefined
        },
        transformRequest: angular.identity          
    })

So you are defining an undefined content-type; You should set multipart/form-data Try to put this content-type

I hope it's usefull

Angelo

Angelo Immediata
  • 6,635
  • 4
  • 33
  • 65
  • No, in this case the content type is getting set correctly - not sure but I think this is done by angular somehow. As you can see from the request the Content-Type header is correctly set. – Stefan Falk Feb 04 '17 at 12:59