我有一个网络表单下载链接按钮,在按钮的点击事件上我正在获取数据,然后生成 .XLSX 文件供下载。在文件生成过程中,响应.Clear()叫做,响应内容类型被设定并最终响应.End()叫做。
我需要显示微调器 .gif在那次操作期间。文件生成并弹出文件“打开/保存”对话框后,微调器不应显示。不幸的是,由于我正在更改内容类型并调用 Response.End,因此没有响应返回到页面进行处理。
任何人都可以为这种情况提供一些帮助吗?
我实际上最终找到了一个解决方案不需要使用 iFrame。相反,我使用Cookie,文件生成过程将简单的名称值对写入其中。稍后可以通过 JavaScript 从客户端检查此 cookie,以确定响应(.xlsx 文件)何时完成。
结果应该是加载旋转图像应该显示,直到 .xls 文件被加载。生成并返回到客户端(Cookie 包含下载完成=true名称值对)。
-
In the 客户端点击时活动为链接按钮:
function startFileDownload(){
// Set Timout for calling checkState function again
setTimeout(checkState, 1100);
setCookie('DownloadComplete', '', 1);
// Download is starting, Hide Download LinkButton
document.getElementById('<%= btnDownloadExcel.ClientID%>').style.display = "none";
// Display progress spinner
var img = document.getElementById("image1");
img.src = "Images/99.GIF";
document.getElementById('image1').className = "spinnerDisplay";
}
-
这是 JavaScript 代码检查状态功能:
function checkState()
{
var img = document.getElementById("image1");
var finished = getCookie("DownloadComplete");
// Check to see if download is complete
if (!isEmpty(finished)) {
setCookie('DownloadComplete', '');
// Download is complete, Hide progress spinner
img.className = "spinnerHide";
document.getElementById('<%= btnDownloadExcel.ClientID%>').style.display = "";
} else {
// Refresh progress spinner, Set Timout for calling checkState function again
img.src = "Images/99.GIF";
setTimeout(checkState, 1100);
}
}
-
这是 JavaScript 代码设置Cookie和获取Cookie功能:
function setCookie(cName, value){
var now = new Date();
var time = now.getTime();
time += 3600 * 1000;
now.setTime(time);
document.cookie = cName + "=" + value
+ '; expires=' + now.toGMTString() + '; path=/';
}
function getCookie(cName)
{
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == cName) {
return unescape(y);
}
}
}
-
然后在清除 HTTP 响应后,在生成 .xlsx 文件的类中的服务器端将名称值对添加到 Cookie:
HttpContext.Current.Response.Clear();
// Append a cookie that will tell the browser the file has finished processing
// and is included in that stream (note specific path must match same path for cookie set in JavaScript)
HttpCookie cookie = new HttpCookie("DownloadComplete", "true");
cookie.Expires = DateTime.Now.AddMinutes(60);
cookie.Path = "/";
HttpContext.Current.Response.AppendCookie(cookie);
//Other code here to specify the MIME type, setup the HTTP header
//and Response.BinaryWrite out the file
HttpContext.Current.Response.End();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)