ajaxFileUpload上传文件

解决前台在上传大文件时会造成的页面长时间无法响应,或者像ajax请求那样上传成功做响应的处理就可使用此插件,ajax目前是无法上传文件的,插件只是实现了类似的异步效果,原理就是在上传文件时动态将选中文件的input移到一个临时创建的你看不到的iframe中,同时再复制一个文件上传input放在原来的位置上,由临时的iframe发起上传请求,所以主页面不会同步卡死,同时监听临时iframe内容,当返回新内容时(即上传成功后台返回数据)再将内容返给主页面

前台Html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
<script type="text/javascript" src="js/ajaxfileupload.js"></script>
</head>
<body>
	<img id="loading" src="image/load.gif" style="display: none;background:#ccc;"> 
		User:<input name="username" value="user" /><br />
		<span id="file">File:<input id="fileToUpload" type="file" name="upload" multiple /></span><br />
		<input id="upload" type="button" value="upload" οnclick="return ajaxFileUpload();" />
		<input type="button" value="alert" οnclick="alert('zzzzzzzzzzzzzz');" />
		<button οnclick="testJSON();" style="width:54px;height:22px;">JSON</button>
		<div id="text">
		<h2>后台测试文本</h2>
		</div>
	<script type="text/javascript">
		
		function testJSON(){
			$.getJSON("test_testJSON",{},function(data){
				$("#text").append(JSON.stringify(data));
			});
		}
		
		function ajaxFileUpload() {  
			/* if($("#file").val().length<1){
				alert("上传文件不能为空!!!");
				return false;
			} */
	        $("#loading").show();
	        $("#upload").attr("disabled","disabled");
	        $("#file").hide();
	        $.ajaxFileUpload({  
	            url : 'test_upload',// servlet请求路径  
	            secureuri : false,  
	            fileElementId : 'fileToUpload',// 上传控件的id  
	            dataType : 'json',  
	            data : {username : $("#username").val()}, // 其它请求参数  
	            success : function(data, status) {  
	                    alert(JSON.stringify(data));
	        	        $("#loading").hide();
	        	        $("#upload").removeAttr("disabled");
	        	        $("#file").show();
	            },  
	            error : function(data, status, e) { 
	                alert('上传出错');  
	            }  
	        })  
	        return false;  
	    }  
		
	</script>
</body>
</html>
后台Action

@Controller("testAction")
@Scope("prototype")
public class TestAction extends ActionSupport
{
  private File upload;
  private String uploadFileName;
  private String uploadContentType;
  private Object ret;

  public String toUpload()
  {
    return "upload";
  }

  public String upload() {
    System.out.println(this.upload.length());
    System.out.println(this.uploadFileName);
    System.out.println(this.uploadContentType);
    this.ret = "upload successful!!!";
    return "ajax";
  }

  public String testJSON()
  {
    List list = new ArrayList();
    for (int i = 0; i < 100000; i++) {
      list.add("stuno:" + i + ";name:name" + i);
    }
    this.ret = list;
    return "ajax";
  }

  public File getUpload() {
    return this.upload;
  }

  public void setUpload(File upload) {
    this.upload = upload;
  }

  public String getUploadFileName() {
    return this.uploadFileName;
  }

  public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = uploadFileName;
  }

  public String getUploadContentType() {
    return this.uploadContentType;
  }

  public void setUploadContentType(String uploadContentType) {
    this.uploadContentType = uploadContentType;
  }

  public Object getRet() {
    return this.ret;
  }

  public void setRet(Object ret) {
    this.ret = ret;
  }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值