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.