Announcement Announcement Module
No announcement yet.
Need help getting MultipartFile Upload to work. Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help getting MultipartFile Upload to work.

    First post, and its a bit drawn out so please bear with me ...

    I am trying to add support for File Upload to an existing Spring 3.1 framework, but am having a great deal of difficulty getting the file to upload.

    The form the file is submitted to my controller from includes a large amount of data in addition to the file.

    What is happening is when I include enctype="multipart/form-data" in my form element, none of the form class members(including the file member) bind (= null) in my controller. If I don't include the enctype in the form element, then all the form class members do bind, but the file member still doesn't.

    Below are what I think are the only pertinent sections of code to this issue. I can't believe that Spring won't allow passing a File along with other data in a Form to a Controller.

    I am using CommonsMultipartResolver and I believe I have the bean defined correctly in my dispatcher servlet:
        <bean id="multipartResolver"
    The pertinent code snippets from the form are:
    <form:form action="${pageContext.request.contextPath}/messages/send.htm" method="POST" id="message" commandName="message" enctype="multipart/form-data">
                    <div id="user-attach-div" class="addon_desc">
                        <div onclick="ShowContent('attach-help')" class="help-info"></div>
                        <form:input type="file" name="file" path="fileToUpload"/>
                        <div class="clear"></div>
    The pertinent code from the controller is:
        @RequestMapping(value="/messages/send.htm", method=RequestMethod.POST)
        public String doSendMessage(
                @ModelAttribute("message") MessageForm message,
                BindingResult result,
    //            @RequestParam(value="file", required=false) CommonsMultipartFile fileToUpload,
                ModelMap model) {
            String command = message.getButton();
            if (StringUtils.isBlank(command)) {
    //            message.setFileToUpload(fileToUpload);
                List<String> errors = processMessageSend(message);
                if (!errors.isEmpty()) {
                    model.addAttribute("error", errors);
                    return "sendmessage.jsp";
            return "redirect:/messages/view.htm";
    The MessageForm includes the CommonsMultipartFile fileToUpload member, getter & setter.

    I think that's all of the pertinent code ... any ideas/suggestions would be greatly appreciated.
    Last edited by gmb; Mar 9th, 2012, 04:06 PM.