JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简单集成教程

 

JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简单集成教程  

JS文件在最下面:

引入

<script src="${path}/js/jquery-2.0.3.min.js"></script>
<script src="${path}/js/jquery.cookie.min.js"></script>

  

html部分 遮盖层

	<div id="loding" style="position: fixed;font-weight:400;font-size:21px;color:#FFFFFF;z-index:200;width: 100%;height: 100%;text-align:center;line-height:600px; background: rgba(0,0,0,0.4);display: none;">
	<img style="vertical-align:middle" src="${path }/image/loding/lodingsafe.gif"  />
	<span >正在导出中请稍后...</span>
	</div>	

  

JS部分

<script type="text/javascript"> 
	 		
	//下载监测timer
	 function initDownloadCheckTimer(){
	 	configDownloadCheckTimer = window.setInterval(function() { 
	 		
	 		var cookieValue = getCookie("downloadCookie");
	 		if (cookieValue === "coursewareExcelDownloadFile") { 
	 			$('#loding').css('display','none');
	 			finishDownload() ;
	 		} 
	 	}, 1000); 
	 }

	 function finishDownload() { 
	 	window.clearInterval(configDownloadCheckTimer); 
	 	$.removeCookie('downloadCookie');
	 	
	 }
	 function getCookie(cookieName) {
         var strCookie = document.cookie;
         var arrCookie = strCookie.split("; ");
         for(var i = 0; i < arrCookie.length; i++){
             var arr = arrCookie[i].split("=");
             if(cookieName == arr[0]){
                 return arr[1];
             }
         }
         return "";
     }
function mySubmit(){
		 
		 $.removeCookie('downloadCookie');
		 $('#loding').css('display','block');
		   initDownloadCheckTimer();
		 
		 
		 var coursewares=document.getElementById("coursewares").value;
		
		 if(coursewares == null || coursewares == ""){
				alert("必须填写ID");	//如果值为空,提示用户填写
				return;
		}else{//如果值不为空,提交表单
			 window.location.href = "${path}/ssss_dddd.action?courseware="+coursewares;

			}

     }
</script>

  

后台部分 JSP项目 可以自己看着改

//        //生成返回cookiee
		Cookie cookie=new Cookie("downloadCookie","coursewareExcelDownloadFile");
		HttpServletResponse response = super.getResponse();
		response.addCookie(cookie);

  

 

 

遮盖层效果: 转圈的小图标 自己百度找一下

 

 

 

jquery.cookie的js
/*! jquery.cookie v1.4.1 | MIT */
!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):"object"==typeof exports?a(require("jquery")):a(jQuery)}(function(a){function b(a){return h.raw?a:encodeURIComponent(a)}function c(a){return h.raw?a:decodeURIComponent(a)}function d(a){return b(h.json?JSON.stringify(a):String(a))}function e(a){0===a.indexOf('"')&&(a=a.slice(1,-1).replace(/\\"/g,'"').replace(/\\\\/g,"\\"));try{return a=decodeURIComponent(a.replace(g," ")),h.json?JSON.parse(a):a}catch(b){}}function f(b,c){var d=h.raw?b:e(b);return a.isFunction(c)?c(d):d}var g=/\+/g,h=a.cookie=function(e,g,i){if(void 0!==g&&!a.isFunction(g)){if(i=a.extend({},h.defaults,i),"number"==typeof i.expires){var j=i.expires,k=i.expires=new Date;k.setTime(+k+864e5*j)}return document.cookie=[b(e),"=",d(g),i.expires?"; expires="+i.expires.toUTCString():"",i.path?"; path="+i.path:"",i.domain?"; domain="+i.domain:"",i.secure?"; secure":""].join("")}for(var l=e?void 0:{},m=document.cookie?document.cookie.split("; "):[],n=0,o=m.length;o>n;n++){var p=m[n].split("="),q=c(p.shift()),r=p.join("=");if(e&&e===q){l=f(r,g);break}e||void 0===(r=f(r))||(l[q]=r)}return l};h.defaults={},a.removeCookie=function(b,c){return void 0===a.cookie(b)?!1:(a.cookie(b,"",a.extend({},c,{expires:-1})),!a.cookie(b))}});

  

posted @ 2020-07-07 10:13  阿浩呗  阅读(1439)  评论(0编辑  收藏  举报