空列表当尝试在Spring中使用ng-file上传许多文件时

时间:2022-05-12 16:03:49

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.

但是,文件列表总是空的,尽管request包含这些文件。

If I add the third MultipartRequest parameter to the method:

如果向方法中添加第三个MultipartRequest参数:

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

I can see it contains my uploaded files correctly:

我可以看到它正确地包含了我上传的文件:

空列表当尝试在Spring中使用ng-file上传许多文件时

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.

我正在使用ng-file上载提交文件,但我不认为它与问题有关。4.2.4的春天。

3 个解决方案

#1


17  

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.

问题是,ng-file-upload默认使用name file[0]、file[1]等提交文件数组。使用Upload服务时,可以使用arrayKey值进行配置。将其设置为空字符串将强制将文件发送到相同的文件键下,该文件键通过Spring和@RequestParam(“file”)列表 正确解析,其中包含已提交的所有文件。

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

#2


3  

Try to use @ModelAttribute like this:

尝试使用如下的@ModelAttribute:

    @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;
      }
   }

#3


0  

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:

我认为在您从前端发送数据的方式中,它不能与java.util.List绑定。如果您创建一个JSON数据作为请求,并使用@RequestBody注释您的列表,如:

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

this should work. Some info here.

这应该工作。一些信息。

#1


17  

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.

问题是,ng-file-upload默认使用name file[0]、file[1]等提交文件数组。使用Upload服务时,可以使用arrayKey值进行配置。将其设置为空字符串将强制将文件发送到相同的文件键下,该文件键通过Spring和@RequestParam(“file”)列表 正确解析,其中包含已提交的所有文件。

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

#2


3  

Try to use @ModelAttribute like this:

尝试使用如下的@ModelAttribute:

    @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;
      }
   }

#3


0  

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:

我认为在您从前端发送数据的方式中,它不能与java.util.List绑定。如果您创建一个JSON数据作为请求,并使用@RequestBody注释您的列表,如:

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

this should work. Some info here.

这应该工作。一些信息。