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)
@PreAuthorize(...)
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 MultipartRequest
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:
What might be the reason of empty List<MultipartFile>
?
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.
I think that in the way you sent data from front, it can not bound with java.util.List. If you create a JSON data as request and you annotated your List with @RequestBody like:
this should work. Some info here.
Try to use
@ModelAttribute
like this:And create a class like:
That works for me, sending big 'email' object with multiple file attachments from UI to back-end:
Angular
Java Spring
The problem was that ng-file-upload by default submits array of files using names
file[0]
,file[1]
etc. It is configurable with thearrayKey
value when usingUpload
Service. Setting it to empty string forces the files to be sent under the samefile
key, which is correctly resolved with Spring and the@RequestParam("file") List<MultipartFile>
contains all files that has been submitted.