`
lt0604
  • 浏览: 15998 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

JQuery.from.js Ajax上传附件

阅读更多
使用JQuery.form插件实现无跳转上传。

以下附上简单实现。

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

    表单提交脚本:

$(document).ready(function() {
    var options = { 
        target:        '#output2',
        beforeSubmit:  showRequest,
        success:       showResponse
    };
    $('#form1').submit(function() {
        $(this).ajaxSubmit(options);
        return false;//阻止表单提交
    });
    function showRequest(formData, jqForm, options) {
        var queryString = $.param(formData); 
        alert('queryString==' + queryString); 
        return true; 
    }
    function showResponse(responseText, statusText) {
        alert('status: ' + statusText + '\n\nresponseText:'+responseText);
    }
});

    html表单:

<form action="MyJsp.jsp" method="post" name="form1" id="form1" enctype="multipart/form-data">
    	<input type="file" id="file1" name="file1"/><br/>
    	<input type="text" id="username" name="username" value=""/>
    	<input type="submit" name="submitButton" value="update">
    	<div id="output2"></div>
</form>

    java处理代码:

DiskFileItemFactory factory = new DiskFileItemFactory();
String filePath = application.getRealPath("/upload");
factory.setRepository(new File(filePath));
factory.setSizeThreshold(1024*1024);
//创建新的文件上传处理类
ServletFileUpload upload = new ServletFileUpload(factory);

try {
    List list = upload.parseRequest(request);   //处理请求
    for(FileItem item : list){
        if(item.isFormField()){//表单元素
        	   //String name=item.getFieldName();
    	   //System.out.println("value==>"+item.getString());
        }
        else {
            String name=item.getFieldName();
            String value=item.getName();
            int start=value.lastIndexOf("\\");
            String fileName=value.substring(start+1);
            System.out.println("fileName=>"+fileName);
            item.write(new File(filePath,fileName));
        }
    }
    response.setContentType("text/html");
    response.setCharacterEncoding("GBK");
    response.setHeader("cache-control","no-cache");
    PrintWriter out = response.getWriter();
    out.print("OK");
    out.flush();
    out.close();
} catch (FileUploadException e) {
    e.printStackTrace();
}

分享到:
评论
1 楼 zhoujj303030 2009-02-25  
不知道具体怎样调用

相关推荐

    jquery.unobtrusive-ajax.min.js

    Ajax.BeginForm 提交,需要引用此文件才会执行OnSuccess

    jquery.form.min.js

    jquery.from.min.js-3.53.0 发现网上一个这文件都收费无语了,有需要的人拿去吧

    jQuery.XDomainRequest.js

    Implements automatic *Cross Origin Resource Sharing* support using the `XDomainRequest` object for IE8 and IE9 when using the [$.ajax](http://api.jquery.com/jQuery.ajax/) function in jQuery 1.5+. ...

    jquery-3.7.0.min.js(jQuery下载)

    jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)...

    jquery.qrcode.min.js---二维码的jquery插件

    jquery.qrcode.min.js 二维码的jquery插件

    Jquery.json.js

    在官网上一直下载不下来 然后共享在这 jquery.json-2.3.min.js和jquery.json-2.3.js

    jquery.form.js下载

    jquery.form.js jquery.form.js

    jquery.pagination.js 下载

    jquery.pagination.js 下载,优秀的jquery分页插件,使用IP代理国外网站下载而来

    jquery.validate 版本大全

    jquery.validate.1.9.0.min.js jquery.validate.1.12.0.min.js jquery.validate.1.13.1.min.js jquery.validate.1.16.0.min.js jquery.validate.1.14.0.min.js jquery.validate.1.15.1.min.js jquery.validate....

    jsontree (jquery.treeview.js) jQuery插件版 treeview1.0

    jsontree (jquery.treeview.js) jQuery插件版 treeview1.0jsontree (jquery.treeview.js) jQuery插件版 treeview1.0jsontree (jquery.treeview.js) jQuery插件版 treeview1.0jsontree (jquery.treeview.js) jQuery...

    jquery.form.js完美ajax无刷新上传图片插件

    jquery.form.js,这是一个完美提交表单的jquery插件,jquery的ajax无法提交图片,而这个插件可以完美解决,使用方法,将该文件放到jquery后面引入:$('#order_form').ajaxForm(function(a) { var b = eval("(" + a ...

    jquery.marquee.js官方下载

    jquery.marquee.js demo及源文件

    jquery.easing.1.3.min.js

    jquery.easing.1.3.min.js 动画效果js

    jquery.pseudo.js

    jquery.pseudo.js 文件,当初上传好像是兼容IE8

    jquery.nicescroll.min-3.7.6.js

    jquery.nicescroll.min-3.7.6.js

    jquery.waypoints.min.js

    页面滚动监听插件,配合jquery.countup.min.js数字动画插件实现数字滚动动画

    jquery.cookie.js下载

    jquery cookie.js 官方下载,一款优秀的 jquery 插件,提供了非常轻量级、简单、实用的操作 cookie 的方法,包括读写、删除等操作,jquery cookie 路径,jquery cookie 时间,jquery cookie 有效期,jquery cookie ...

    jquery.base64.js下载

    本想免费分享,方便懒人同学的,居然必须2积分才能上传,也是醉了. 官网下载地址:http://plugins.jquery.com/base64/ 有完整实例. 资源上传时为最新版本. 再说一遍, 不想花费积分的可以去官网下载, 上边有地址 yo !

    jquery.form.js和使用说明

    文件压缩包里有jquery.form.js和使用说明文档 jquery表单验证插件_jquery.form.js

    基于jquery.print.js插件的js打印demo

    基于jquery.print.js插件的js打印demo,简单易用,使用详细教程请查看我的个人主页的博客《一款很好用的Jquery 打印插件——jQuery.print.js》

Global site tag (gtag.js) - Google Analytics