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

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

问题描述

我有以下控制器方法一次上传多个文件,受到提交文件,但我认为它没有连接有问题。 Spring 4.2.4。

解决方案

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

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

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

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