表单提交

表单提交:

1 使用submit提交按钮
<input type="submit" value="submit"\>

2 使用submit函数提交
$("formid").submit();
$("form").submit(function(e){
    alert("Submitted");
  });
可以定义一个函数,当表单提交时执行的函数,这个只是适用于表单提交

3 ajax方法
 $.ajax({                
        cache: true,                
        type: "POST",                
        url:"servlet/imageupload",                     
        async: false,                          
        success: function(data) {                    
         alert($('#uploadform').serialize());               
         }            
        });
这样可以实现提交后执行的函数

文件上传:
最简单的方式是使用ajaxfileupload.js插件

1 使用submit提交按钮
<input type="submit" value="submit"\>
2 2 使用submit函数提交
$("formid").submit();
这里submit不能带有参数

3 如果想实现提交后执行函数,比如图片上传后在本地展示

具体说明submit提交后,targent指向iframe,iframe刷新执行相关的函数

 function uploadImage()
  {
 
 
         
            $('#uploadform').submit();
            jQuery('#' +  "uploadiframe").load(uploadCallback   );
           

 
 
  }
   function uploadCallback()
   {
    var io = document.getElementById("uploadiframe");
    var xml={};
    var data;
    if(io.contentDocument)
                {
                     xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
                    xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
                } 
    else if(io.contentWindow)
                {
                     xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
                     xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
                    
                }
     data =  xml.responseText;
 
    data = eval( "(" +data+")" );//eval("data="+data);
    $("#simulate_map").attr("src",data["path"]).show();
   }

jsp

 <form action="servlet/imageupload" method="post" enctype="multipart/form-data" id="uploadform" target="uploadiframe">
   <div >
   <input type="file" name="file1" οnchange="uploadImage()"/>
 
    <div
     style="height: 140px; width: 220px; margin:auto;position:relative;">
    <img id="simulate_map" style="width: 220px; height: 140px; " />
   </div>
   </div>
  
   </form>
    <iframe name="uploadiframe" id="uploadiframe" style="display:none">
   </iframe>

java

 private void upload(HttpServletRequest request, HttpServletResponse response) {
        // 用于存放上传文件的目录
        String directory = "TEMP";
        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(4096);
      
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding("utf-8");
        upload.setSizeMax(100 * 1024 * 1024);

        try {
            List<?> items = upload.parseRequest(request);
            Iterator<?> it = items.iterator();
           
         
            while (it.hasNext()) {
                FileItem item = (FileItem) it.next();
                if (!item.isFormField()) {
                    int length = item.getName().replace('\\', '/').split("/").length;
                    String name = item.getName().replace('\\', '/').split("/")[length - 1];
                    System.out.println(name);
                    File dir = new File(request.getSession().getServletContext().getRealPath(directory));
                    if (!dir.isDirectory() && !dir.mkdirs()) {
                        return;
                    }
                    File putFile = new File(request.getSession().getServletContext().getRealPath(directory)
                        + File.separator + name);
                    // 写文件
                    item.write(putFile);
                    // 设置返回响应
                  response.setContentType("text/html");
                    response.setStatus(HttpServletResponse.SC_OK);

                    String basePath = request.getScheme() + "://" + request.getServerName() + ":"
                        + request.getServerPort() + request.getContextPath() + "/";
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("path", basePath + "/" + directory + "/" + name);
                    PrintWriter out = response.getWriter();
                    out.write(jsonObject.toString());
                    out.flush();
                    out.close();

                }

            }

        } catch (Exception e) {
            // TODO: handle exception
        }

    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值