空列表<MultipartFile>尝试使用 ng-file-upload 在 Spring 中上传许多文件时 [英] Empty List&lt;MultipartFile&gt; when trying to upload many files in Spring with ng-file-upload

查看:20
本文介绍了空列表<MultipartFile>尝试使用 ng-file-upload 在 Spring 中上传许多文件时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器方法可以一次上传多个文件,灵感来自 提交文件,但我不要认为它与问题有关.春季 4.2.4.

解决方案

问题在于 ng-file-upload 默认使用名称 file[0]file[1] 等提交文件数组.它可以使用 arrayKey 使用 Upload 服务时的值.将其设置为空字符串会强制文件在相同的 file 键下发送,这可以通过 Spring 和 @RequestParam("file") List 正确解析包含已提交的所有文件.

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

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.

解决方案

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: ''})

这篇关于空列表<MultipartFile>尝试使用 ng-file-upload 在 Spring 中上传许多文件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆