使用Spring MVC尝试上传多部分文件以获取特定div中的响应,但多部分文件在控制器中返回null

时间:2012-11-26 10:48:40

标签: html spring-mvc

  1. 我在我的调度程序servlet中配置了一个多部分解析程序。 st item
  2. 我希望结果能够反映在特定的div中。
  3. 我使用ajax实现了特定div响应的动机,但是我的图像文件没有到达控制器,因此错误图像上传返回null
  4. 图像文件无法到达控制器并返回null。
  5. 我希望控制器能够收到我的图像文件。 并且响应应该在名为“harryPAGE”
  6. 的div上

    我的jsp表单页面是

     <form:form method="post" modelAttribute="uploadBean"
        action="url"//this action takes to controller with request mapping as "hello"
         commandName="uploadBean">
        <table class="standardTable">
    
            <tr>
                <td>File</td>
                <td><input type="file" name="fileData" id="fileData" size="50"
                     required="required"></td>
            </tr>
            <tr>
                <td colspan=2 align="left"><input
                    class="btn right breadPrevious" type="submit" value="Upload"
                     /></td>
    
    
            </tr>
    
        </table> 
    
    <script>
    
        $.ajax({
            cache: false,
            contentType: false,
            processData: false,
            type: 'POST',
        });
    
        $("form").submit(
    
                function() {
                    alert("hi");
                    var value = $("#fileData");
                    alert(value);
    
                    $.post($(this).attr("action"), $(this).serialize(),
                            function(html) {
                                $("#harryPAGE").html(html);
                            });
                    return false; // prevent normal submit
                });
    
    
    
        </script>
        <c:out value="${message}"></c:out>
    </form:form>
    

    我的控制器类是

    @RequestMapping(value = "/hello", method = RequestMethod.POST)
    public  ModelAndView uploadImage(@ModelAttribute ImageUploadBean uploadBean,HttpServletRequest req) {
        int test=5; 
    
        ModelAndView modelAndView = new ModelAndView("apage");
        try {
            System.out.println(req.getParameter("fileData"));
            MultipartFile imgFile = uploadBean.getFileData();
    
            test=imageService.uploadImage(imgFile);
            if(test==1){
    
                modelAndView.addObject("message","some message");           }
            else if(test==0){
    
                modelAndView.addObject("message","some message");           }               
        } catch (Exception e) {//the control enters here and gives exception
            logger.error("Unable to upload image " + e.getMessage(), e);
            modelAndView.addObject("message","some error");     }
        return modelAndView;
    }
    

    我的调度程序servlet .xml文件是我配置了多部分解析程序

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">     
    <!-- one of the properties available; the maximum file size in bytes -->   
    <property name="maxUploadSize" value="100000000"/>  
    </bean>  
    

1 个答案:

答案 0 :(得分:0)

只需在表单中添加enctype="multipart/form-data"属性:)