form 表单提交数据和文件(fromdata的使用方法)

<!--  数据和文件一次性提交  -->
<form class="form_meren" id="mainForm" name="mainForm" action="${ctx}/shahescenicinfo/insertShaheScenicinfo.do" method="post" enctype="multipart/form-data">

       <input class="form-control " id="aname" name="aname" type="text">
       <input type="file" id="otherfiles">
<input type="file" id="otherfiles2">
 
<button type="button" οnclick="sbutripform()" >保存</button> </form>
//验证结束后执行保存
        function sbutripform(){
                       //form的id就是mainForm
            var mainstr=formToJsonObject(mainForm);     
                       //from表单对象 转json
             mainstr=JsonToStr(mainstr);
             mainstr=mainstr.replace("%", "%25");
             var manstr = mainstr.toString();
             var formData = new FormData();
              formData.append("file1",document.getElementById("otherfiles").files[0]);
              formData.append("file2",document.getElementById("otherfiles2").files[0]); 
              formData.append("jsonStr",manstr);
             $.ajax({                  
                    url:url,
                    type:'POST',
                    dataType:'json',
                    data:formData,
                    cache: false,//上传文件无需缓存
                   processData: false,//用于对data参数进行序列化处理 这里必须false
                   contentType: false, //必须
                    success:function(xclyid){
                         window.top.customAlertTip("", "保存成功", "warning");
                     },
                     error:function (error){
                           window.top.customAlertTip("", "保存失败", "warning");
                     }
            });
        
        }
@RequestMapping(value="/updateallareainfo.do", method={RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public String updateallareainfo(HttpServletRequest request,
        @RequestParam(value="file1",required=false)MultipartFile file1,
        @RequestParam(value="file2",required=false)MultipartFile file2,
        String jsonStr) {
        //把数据从json 转换为 实体类
        YbsjAreaInfo ybsjAreaInfo = JSON.parseObject(jsonStr,YbsjAreaInfo.class);
      try {
            log.info("当前访问的action方法:updateallareainfo") ;
             // uploads   文件夹位置
             String rootPath = request.getSession().getServletContext().getRealPath(PropertyManager.getProperty("voiceupload"));

              // uploads  文件夹2位置
              String imgPath = request.getSession().getServletContext().getRealPath(PropertyManager.getProperty("filesavepath

// 原始名称
            String newFileName1 = "";
            String newFileName2 = "";

       if (file1!=null) { String originalFileName1 = file1.getOriginalFilename();//旧的文件名(用户上传的文件名称) //新的文件名 newFileName1 = UUIDUtil.getuuid() + originalFileName1.substring(originalFileName1.lastIndexOf(".")); File newFile = new File(rootPath + File.separator + newFileName1); // 判断目标文件所在目录是否存在 if( !newFile.getParentFile().exists()) { // 如果目标文件所在的目录不存在,则创建父目录 newFile.getParentFile().mkdirs(); } //存入 file1.transferTo(newFile); ybsjAreaInfo.setMapUrl("/"+filesavepath+newFileName1); } if (file2!=null) { String originalFileName2 = file2.getOriginalFilename(); newFileName2 = UUIDUtil.getuuid() + originalFileName2.substring(originalFileName2.lastIndexOf(".")); File newFile2 = new File(imgPath + File.separator + newFileName2); if( !newFile2.getParentFile().exists()) { // 如果目标文件所在的目录不存在,则创建父目录 newFile2.getParentFile().mkdirs(); } file2.transferTo(newFile2); ybsjAreaInfo.setAlogo("/"+filesavepath+newFileName2); } } catch (IllegalStateException | IOException e) { e.printStackTrace(); } int updateallareainfo = ybsjAreaInfoService.updateallareainfo(ybsjAreaInfo); String shaheScenicinfoid="{\"id\":\""+updateallareainfo+"\"}"; return shaheScenicinfoid; }

 以上是 单个文件 接收的方法

 

 

 

 

 

如果用到一个Input 上传多个文件   

后台用

@RequestParam("files") MultipartFile[] files 

接收就好了


转载于:https://www.cnblogs.com/gjths/p/11244282.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值