美文网首页
form表单中带有文件上传,同时提交

form表单中带有文件上传,同时提交

作者: 唐怀瑟_ | 来源:发表于2018-08-03 09:12 被阅读778次

    前台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";
            }
    

    相关文章

      网友评论

          本文标题:form表单中带有文件上传,同时提交

          本文链接:https://www.haomeiwen.com/subject/hazlvftx.html