这个问题困扰了好半天,开始想着页面接收到返回值后关闭页面遮罩层,但是下载打印的很难控制返回值,后上网查询了一下,看到可以用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);
}