关于form表单如何与文件同时提交的问题

前台JS(easyUI)

function EditsubmitForm() { 
    var father = $('#select_category').combobox('getValue');
    $('#knowledgeAddForm').form('submit', { 
       url: "${path }/knowledgeAdd/addPageKnowledgeAdd?fatherId="+father, 
       onSubmit: function () {        //表单提交前的回调函数 
              var isValid = $(this).form('validate');//验证表单中的一些控件的值是否填写正确,比如某些文本框中的内容必须是数字 
              return isValid; // 如果验证不通过,返回false终止表单提交 
       }, 
       success: function () {  //表单提交成功后的回调函数,里面参数data是我们调用/BasicClass/ModifyClassInfo方法的返回值。 
               parent.$.messager.alert("添加成功","知识库添加成功!",'info');
               location.reload();
            } 
          });       
    }

前台表单

<form enctype="multipart/form-data" id="knowledgeAddForm" method="post" >
   <table class="grid">
      <tr>
            <td>内容</td>
            <td>
                  <textarea name="content" class="easyui-validatebox" data-options="required:true" value="${Knowledge.content}"></textarea>
            </td>
        </tr>
         <tr>
              <td>上传</td>
              <td>
                     <input id ="file" type="file" name="file" size="10">
              </td>
         </tr>
   </table>
       <div style="margin-top: 10px;"> 
            <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" style="margin-left: 0px;" onclick="EditsubmitForm()">确定</a> 
            <!-- <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" style="margin-left: 60px;" onclick="EditclearForm()">取消</a>  -->
      </div>
</form>

后台controller层

@RequestMapping(value = "/knowledgeAddPage", method = RequestMethod.GET)
        public String addPage() {
            return "/knowledge/knowledgeAdd";
        }
        @RequestMapping(value = "/addPageKnowledgeAdd")
        public String addKnowledge(@RequestParam(value = "file") MultipartFile file,Knowledge knowledge,Long fatherId,HttpServletRequest req) throws IOException {
            String domain = req.getSession().getAttribute("domain").toString();
            String userName = req.getSession().getAttribute("username").toString();
            String fileName = file.getOriginalFilename();
            if(fileName==""){
                knowledge.setDomain(domain);//域名
                knowledge.setUserName(userName);//当前用户名
                knowledge.setFatherId(fatherId);//关联ID
                knowledgeAddService.addKnowledge(knowledge);
            }else{
                String suffix = fileName.substring(fileName.lastIndexOf(".") + 0);
                Date d2 = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
                String dateNumber = sdf.format(d2);
                String attachment = "D://"+dateNumber+suffix;
                File desc = MakeFileUtil.makefile(attachment);
                knowledge.setDomain(domain);
                knowledge.setUserName(userName);
                knowledge.setFileName(fileName);//文件名
                
                knowledge.setAttachment(attachment);//文件路径
                knowledge.setFatherId(fatherId);
                knowledgeAddService.addKnowledge(knowledge);
                
                file.transferTo(desc);//上传
                
            }
            return "/knowledge/knowledgeAdd";
        }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值