weixin_33698823 2014-12-16 09:14 采纳率: 0%
浏览 84

Ajax发送多部分/表单数据

My html code:

<form:form name="vcfForm" id="vcfForm" method="post" enctype="multipart/form-data" action="../acquaintance/readingContactsFromVcfFile"></form:form>

<input type="file" name="vcfFile" id="vcfFile" form="vcfForm" >
    <button type="button" name="vcfSubmit" id="vcfSubmit" form="vcfForm">Upload</button>

My controller :

@RequestMapping(value = { "/readingContactsFromVcfFile" }, method = RequestMethod.POST)
public @ResponseBody
ModelMap readContactsFromVcfFile(@RequestParam("vcfFile") MultipartFile file, HttpServletRequest request) throws UserServiceException {
    ModelMap modelMap = new ModelMap();
  *********************code*****************
  modelMap.addAttribute("message", "success");
  return modelMap;
}

My jquery code:

 $(document).on('click','#vcfSubmit', function() {
                        var vcfData = new FormData(); 
                        vcfData.append('files[]', $('#vcfForm').get(0).files[0]);
                        $.ajax({
                            url : "../acquaintance/readingContactsFromVcfFile?vcfFile="+vcfData,
                            type : "post",
                            cache : false,
                            processData: false,
                            contentType: false,
                            success : function(data) {
                            alert(data.message);                                        
                            }
                            });   
                    });  

My problem is when i click submit button the page will submitted and return message "success" displayed in page. I want page not refresh when submitting the form or pass file using ajax. How can i solve this problem?

  • 写回答

1条回答 默认 最新

  • 程序go 2014-12-16 13:04
    关注

    html code:

    <form name="vcfForm" id="vcfForm" method="post" enctype="multipart/form-data" ></form>
    <input type="file" name="vcfFile" id="vcfFile" form="vcfForm" >
        <button type="button" name="vcfSubmit" id="vcfSubmit" form="vcfForm">Upload</button>
    

    controller code:

    @RequestMapping(value = { "/readingContactsFromVcfFile" }, method = RequestMethod.POST)
    public @ResponseBody
    ModelMap readContactsFromVcfFile(@RequestParam(value = "vcfFile") MultipartFile file, HttpSession session) throws UserServiceException {
        ModelMap modelMap = new ModelMap();
        modelMap.addAttribute("message", "message");
        return modelMap;
    }
    

    jquery code:

    $(document).on('click','#vcfSubmit',function(){
                             var vcfData = new FormData($('#vcfForm')[0]); 
                              $.ajax({
                                    url : "../acquaintance/readingContactsFromVcfFile?vcfFile="+vcfData,
                                    type : "post",
                                    data : vcfData,
                                    processData: false,
                                    contentType: false,
                                    cache : false,
                                    success : function(data) {
                                    }
                                });   
                        }); 
    

    Every thing working fine.

    评论

报告相同问题?