How to bind uploaded file to form object property?

Go To StackoverFlow.com

2

I would like to have the form object like this:

public class FormData {

    private MultipartFile file1;

    private MultipartFile file2;

    // setters/getters for above fields and other properties
}

and I would like SpringMVC to bind e.g. <input type="file" name="file1"> HTML form field to my form object property. I haven't found any JSP tag for that (like <form:file path="file1" /> or <form:input type="file" path="file1" />) in spring-form.tld. Is it possible at all? If not, is this limitation rooted too deep in Spring internals and cannot be easily overcome?

I know how to bind file upload form value to controller method argument (described in documentation):

@RequestMapping(value = {"/"}, method = RequestMethod.POST)
public ModelAndView compare(@ModelAttribute(FORM_BEAN_NAME) FormData formData,
    @RequestParam("file1") MultipartFile file1,
    @RequestParam("file2") MultipartFile file2,
    BindingResult bindingResult) {

but I would be happier if all HTML form elements are bound to one class and method signature does not boost when new upload fields are added.

2012-04-05 17:52
by dma_k


1

I did exactly this a couple of years ago. The only difference I can remember is I used CommonsMultipartFile instead of an interface. What happens if you try this, it throws an exception or simply values don't get bound?

2012-05-13 13:23
by sinuhepop
Why haven't you used org.springframework.web.multipart.MultipartFile interface for that which is more generic - dma_k 2012-05-14 09:34
I don't remember the reason, maybe it doesn't has sense. Have you tried it? What happens - sinuhepop 2012-05-14 11:10
Thanks, it looks that bindin - dma_k 2012-05-14 12:46
Ads