fracz fracz - 1 year ago 181
AngularJS Question

Empty List<MultipartFile> when trying to upload many files in Spring with ng-file-upload

I have the following controller method for uploading multiple files at once, inspired by this blog post and answers to this question as well:

@RequestMapping(value = "/{user}/attachment", method = RequestMethod.POST)
public void upload(@PathVariable User user,
@RequestParam("file") List<MultipartFile> files) {
// handle files

However, the list of the files is always empty although request contains them.

If I add the third
parameter to the method:

public void upload(@PathVariable User user,
@RequestParam("file") List<MultipartFile> files,
MultipartRequest request)

I can see it contains my uploaded files correctly:

request contents

What might be the reason of empty

I'm using ng-file-upload to submit the files, but I don't think it is connected with the issue. Spring 4.2.4.

Answer Source

The problem was that ng-file-upload by default submits array of files using names file[0], file[1] etc. It is configurable with the arrayKey value when using Upload Service. Setting it to empty string forces the files to be sent under the same file key, which is correctly resolved with Spring and the @RequestParam("file") List<MultipartFile> contains all files that has been submitted.

Upload.upload({url: url, data: {file: arrayOfFiles}, arrayKey: ''})