修改form重定到iframe中,模拟异步上传文件的效果

<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@ page import="gap.rm.tools.helper.RmVoHelper"%>
<%@ taglib uri="reference" prefix="r"%>
<%@page import="sgcc.htjs.contract.util.IContractConstants"%>
<html>
<head>
<%@ include file="/jsp/include/global.jsp"%>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>编辑技术变动单</title>
<script language="javascript">
        var rootPath = '<%=request.getContextPath()%>';//根路径
        $(document).ready(function() {
        alert()
           
        });
    
    
    function upload_free_file(){
        $("#con_edit_form").get(0).action = "<%=request.getContextPath()%>/ConChangeApplyAction.do?cmd=ajaxFormSubmit";
        $("#con_edit_form").get(0).target = "form_iframe";//修改form重定到iframe中,模拟异步上传文件的效果
        try{
            $('#con_edit_form').get(0).encoding = "multipart/form-data"; //IE+
        }catch(err){
            $("#con_edit_form").get(0).enctype = "multipart/form-data";
        }
        $("#con_edit_form").submit();
    }
           $(document).ready(function() {
                $("#form_iframe").get(0).onload = function(){
                debugger;
                //得到返回的json信息等
                    var result_str = $("#form_iframe").get(0).contentWindow.document.body.innerHTML;
                    if(!result_str){
                      result_str = $("#form_iframe").get(0).contentWindow.document.documentElement.innerHTML;
                    }
                    alert(result_str);
                  }
             });
             //save
             function conApplySave(){
                    $("#con_edit_form").get(0).action = "<%=request.getContextPath()%>/ConChangeApplyAction.do?cmd=conApplySave";
                     try{
                        $("#con_edit_form").get(0).enctype = "";
                    }catch(err){
                    }
                     try{
                         $('#con_edit_form').get(0).encoding = "";//IE+
                    }catch(err){
                    }
                    $("#con_edit_form").submit();
             }
             //这种方式能够实现ifram不刷新页面,异步上传文件的效果,并能完成验证。
             /**
             //frame上传验证数据
  public IForward ajaxFormSubmit(DefaultForm formBean, IRequest request, IResponse response) throws Exception {
      String jsonString = "";
      HttpServletRequest hRequest=(HttpServletRequest) request.getServletRequest();
      //建立上传附件解析器,目前只支持默认建立
      FileUploador fu = FileUploadorFactory.createInstance();
      try {
          //判断是否文件二进制提交
            fu.parse(hRequest);
          //设置最大只容许附件大小
            fu.setSizeMax(10*1024*1024*10); //100M
                String test_x = fu.getParamterString("test_x");
                //获取附件对象.
                //若是附件对象不存在,或者大小、类型不符合要求,则返回null
                FileInfo fi = fu.getFileInfo("free_filename");
                if(null != fi){
                    InputStream file_in = fi.getFile();
                    POIFSFileSystem pois = new POIFSFileSystem(file_in);
                    HSSFWorkbook wb = new HSSFWorkbook(pois);
                    HSSFSheet sheet = wb.getSheetAt(0);
                    int length=sheet.getLastRowNum();
                    String str = sheet.getRow(0).getCell(0).toString();
                    System.out.println(str);
                    jsonString = "{'status':'200','contractinfo_id':'121212212','result':'"+str+"'}";
                }
        } catch (Exception e) {
            e.printStackTrace();
            jsonString = "{'status':'203','contractinfo_id':'121212212','result':''}";
        }
        TooolUtil.setTextAjax(jsonString,response);
        return null;
  }
  public IForward conApplySave(DefaultForm formBean, IRequest request, IResponse response) throws Exception {
      String test_x = request.getParameter("test_x");
      System.out.println(test_x);
      return request.findForward("con_change_list");
  }
             */
</script>
</head>
<body>
<script language="javascript">
    writeTableTop('列表页面','<%=request.getContextPath()%>/');  //显示本页的页眉
</script>
        <form  name="con_edit_form" id="con_edit_form" enctype="multipart/form-data" method="post">
            <input type="hidden" name="cmd" value="ajaxFormSubmit"/>
            <input type="text" name="test_x" value=""/>
            <input type="file" name="filename" value=""/>
            <input name="button_return" id="button_return"
                        class="button_ellipse" type="button" value="提交"
                        onclick="javascript:upload_free_file();">javascript

        </form>
        <script language="javascript">
    writeTableBottom('<%=request.getContextPath()%>/');  //显示本页的页脚
</script>
</body>
</html>
<script language="javascript">
<%  //表单回写
    if(request.getAttribute(IContractConstants.REQUEST_WRITE_BACK_FORM_VALUES) != null) {  //若是request中取出的表单回写bean不为空
        out.print(RmVoHelper.writeBackMapToForm((java.util.Map)request.getAttribute(IContractConstants.REQUEST_WRITE_BACK_FORM_VALUES)));  //输出表单回写方法的脚本
    }
%> 
</script>
 html

相关文章
相关标签/搜索