使用JQuery.form插件实现无跳转上传

9 篇文章 0 订阅

转自:http://f1jee.blog.163.com/blog/static/1752221672009102903734838/


使用JQuery.form插件实现无跳转上传。

以下附上简单实现。

准备:commons-fileupload.jar
     commons-io-1.1.jar
     jquery-1.3.1.js
     jquery.form.js

    表单提交脚本:

Js代码
  1. $(document).ready(function() {   
  2.     var options = {   
  3.          target:        '#output2',   
  4.          beforeSubmit:   showRequest,   
  5.          success:        showResponse   
  6.      };   
  7.      $('#form1').submit(function() {   
  8.          $(this).ajaxSubmit(options);   
  9.         return false;//阻止表单提交   
  10.      });   
  11.     function showRequest(formData, jqForm, options) {   
  12.         var queryString = $.param(formData);   
  13.          alert('queryString==' + queryString);   
  14.         return true;   
  15.      }   
  16.     function showResponse(responseText, statusText) {   
  17.          alert('status: ' + statusText + '\n\nresponseText:'+responseText);   
  18.      }   
  19. });  

    html表单:

Html代码
  1. <form action="MyJsp.jsp" method="post" name="form1" id="form1" enctype="multipart/form-data">  
  2.         <input type="file" id="file1" name="file1"/><br/>  
  3.         <input type="text" id="username" name="username" value=""/>  
  4.         <input type="submit" name="submitButton" value="update">  
  5.         <div id="output2"></div>  
  6. </form>  

    java处理代码:

Java代码
  1. DiskFileItemFactory factory = new DiskFileItemFactory();   
  2. String filePath = application.getRealPath("/upload");   
  3. factory.setRepository(new File(filePath));   
  4. factory.setSizeThreshold(1024*1024);   
  5. //创建新的文件上传处理类   
  6. ServletFileUpload upload = new ServletFileUpload(factory);   
  7.   
  8. try {   
  9.      List list = upload.parseRequest(request);   //处理请求   
  10.     for(FileItem item : list){   
  11.         if(item.isFormField()){//表单元素   
  12.                //String name=item.getFieldName();   
  13.            //System.out.println("value==>"+item.getString());   
  14.          }   
  15.         else {   
  16.              String name=item.getFieldName();   
  17.              String value=item.getName();   
  18.             int start=value.lastIndexOf("\\");   
  19.              String fileName=value.substring(start+1);   
  20.              System.out.println("fileName=>"+fileName);   
  21.              item.write(new File(filePath,fileName));   
  22.          }   
  23.      }   
  24.      response.setContentType("text/html");   
  25.      response.setCharacterEncoding("GBK");   
  26.      response.setHeader("cache-control","no-cache");   
  27.      PrintWriter out = response.getWriter();   
  28.      out.print("OK");   
  29.      out.flush();   
  30.      out.close();   
  31. } catch (FileUploadException e) {   
  32.      e.printStackTrace();   



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值