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.

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?