Announcement Announcement Module
Collapse
No announcement yet.
Need help getting MultipartFile Upload to work. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:
    Code:
        <bean id="multipartResolver"
              class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
    The pertinent code snippets from the form are:
    Code:
    <form:form action="${pageContext.request.contextPath}/messages/send.htm" method="POST" id="message" commandName="message" enctype="multipart/form-data">
    and
    Code:
                    <div id="user-attach-div" class="addon_desc">
                        <span>Attachment</span>
                        <div onclick="ShowContent('attach-help')" class="help-info"></div>
                        <form:input type="file" name="file" path="fileToUpload"/>
                        <div class="clear"></div>
                    </div>
    The pertinent code from the controller is:
    Code:
        @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.
Working...
X