I am trying to upload multiple files using spring 3.1.2 with @Controller and @RequestMapping.
Here's what I did and my configuration.
Html5 form :
<form action="addFileSystemImage.foo" method="post" enctype="multipart/form-data">
<input class='fileInput' type="file" name="files[]" multiple="multiple" />
<input type="text" value="13asdf12eadsf" name="locId"/>
<input type="submit" />
</form>
Controller method :
@RequestMapping(value="/publisher/addFileSystemImage.foo", method=RequestMethod.POST)
public @ResponseBody List<UploadedFile> addFileSystemImage(@RequestParam("files[]") ArrayList<MultipartFile> files, String locId, HttpServletRequest request) {
//do lotsa voodoo rocket science here to process the files
}
my conf :
<mvc:annotation-driven />
<context:component-scan base-package="foo.package"></context:component-scan>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
Submitting the form does get to the addFileSystemImage method. The data for locId argument is here, but the "files" argument is not bound. It is systematically null no matter what combination of argument / field names / argument types I have tried.
The HttpServletRequest argument is a org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest and it holds a multiPartFile attribute which actually holds the file data. Looking at its value in debug gives me
{files[]=[org.springframework.web.multipart.commons.CommonsMultipartFile@16afd7f9, org.springframework.web.multipart.commons.CommonsMultipartFile@728c2811, org.springframework.web.multipart.commons.CommonsMultipartFile@4f9aaed7]}
which means my files[] is indeed here ... but somehow it did not pass the data binding step properly ...
Now ... I know you're gonna tell me I can retrieve the data from the request ... but I'd rather have this working properly ... the Sring way... :) and have my ArrayList of MultipartFile properly populated.
Am I missing something ? Has anyone actually made this work properly ? What can I do to have this ArrayList (or even an regular Array ) populated?
I came accross this solution Spring MVC with ajax file upload and MultipartFile which does pretty much the same thing as I am but obviously I must be doing something wrong since this solution is not working for me.
Note : I did manage to get it working with single file uploads. But my challenge today is to get multiple files at once.
Any help appreciated.
Thanks in advance.
Although you've already gotten your answer thanks to Alex, I'd just like to elaborate a bit. With Spring binding, form fields are bound to their "name" attributes in the HTML. Since it is impossible to have a form field named files[] (if one declares a variable name with that syntax, its name is files, but it is an array of the declaring type), Spring couldn't match it up - and the behavior in that case is to disregard the data in the request.
Using a type such as MultipartFile, you can use either a List named "files" or an array as the following examples:
private List<MultipartFile> files;
private MultipartFile[] files;
With appropriate getters and setters, you can then access and mutate the file list accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With