Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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:

request contents

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.

like image 557
fracz Avatar asked Feb 02 '16 17:02

fracz


People also ask

How does MultipartFile work in spring?

The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired. The temporary storages will be cleared at the end of request processing.

What is multipart file upload?

Multipart upload allows you to upload a single object as a set of parts. Each part is a contiguous portion of the object's data. You can upload these object parts independently and in any order. If transmission of any part fails, you can retransmit that part without affecting other parts.

How do I change a file to MultipartFile?

MultipartFile multipartFile = new MockMultipartFile("sourceFile. tmp", "Hello World". getBytes()); File file = new File("src/main/resources/targetFile. tmp"); try (OutputStream os = new FileOutputStream(file)) { os.


2 Answers

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: ''})
like image 166
fracz Avatar answered Sep 18 '22 13:09

fracz


Try to use @ModelAttribute like this:

    @RequestMapping(value = "/{user}/attachment", method = RequestMethod.POST)
    @PreAuthorize(...) 
    public void upload(@PathVariable User user,@ModelAttribute("uploadFile") FileUpload uploadFile) throws IllegalStateException, IOException {

    List<MultipartFile> files = uploadFile.getFiles();
    ...

And create a class like:

     public class FileUpload {
     private List<MultipartFile> files;
     public List<MultipartFile> getFiles() {
        return files;
     }

    public void setFiles(List<MultipartFile> files) {
       this.files= files;
      }
   }
like image 29
Abdelhak Avatar answered Sep 21 '22 13:09

Abdelhak