防止页面多次点击下载(为下载加入遮罩层)

 

这个问题困扰了好半天,开始想着页面接收到返回值后关闭页面遮罩层,但是下载打印的很难控制返回值,后上网查询了一下,看到可以用cookie控制,也想起来以前写的相关代码,试验了一下,简单高效,代码如下:

页面代码:(定时检测cookie,关闭遮罩层)

//下载监测timer
function initDownloadCheckTimer(){
	configDownloadCheckTimer = window.setInterval(function() { 
		var cookieValue = $.cookie('downloadCookie');
		if (cookieValue === "finishedDownloadFile") { 
			$("body").hideLoading();
			finishDownload() ;
		} 
	}, 1000); 
}

function finishDownload() { 
	window.clearInterval(configDownloadCheckTimer); 
	 //clears this cookie value 
	$.removeCookie('configDownloadToken');
}

function download(){
    //校验及准备数据
    。。。。。。
    //开启遮罩层
    $("body").showLoading();
    initDownloadCheckTimer();
    uarl=。。。。。;
    window.location=url;
}

后台只需在response中加入标志cookie就可以了:

Cookie cookie=new Cookie("downloadCookie","finishedDownloadFile");
response.addCookie(cookie);

附:下载excel方法

//创建一个excel
HSSFWorkbook wb = new HSSFWorkbook();
//添加一个sheet
HSSFSheet sheet = wb.createSheet("协谈报表统计");
//添加表头
HSSFRow row = sheet.createRow(0);
//创建单元格,并设置表头
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//居中
int f = 0;
HSSFCell cell = row.createCell(f++);
cell.setCellValue("第一列");
cell.setCellStyle(style);
cell = row.createCell(f++);
cell.setCellValue("第二列");
。。。。。。
try{
    ServletOutputStream fout= response.getOutputStream();
    response.reset();
    response.setContentType("application/vnd.ms-excel");
    String filename="filename.xls";
    filename=new String(filename.getBytes("UTF-8"),"iso-8859-1").trim();
    response.addHeader("Content-Disposition", "attachment;filename="+filename);
    Cookie cookie=new Cookie("configDownloadToken","finishedDownloadFile");
    response.addCookie(cookie);
    wb.write(fout);
    fout.close();
}catch{
    logger.error("下载出错!"+e);
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值