我有在我的项目中下载excel文件的功能。 单击导出按钮时,会显示porgress栏。但即使出现浏览器保存为对话框,进度条也不会被隐藏。 问题是在response.end进度条未使用之后。 在asp.net ajax上可见的进度条开始请求,在结束请求时看不到。代码如下所示。
可见并隐藏进度条:
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function BeginRequestHandler(sender, args) {
// alert('B');
var elem = args.get_postBackElement();
ActivateAlertDiv('visible', 'AlertDiv', elem.value + ' processing...');
}
function EndRequestHandler(sender, args) {
ActivateAlertDiv('hidden', 'AlertDiv', '');
}
function ActivateAlertDiv(visstring, elem, msg) {
var adiv = $get(elem);
adiv.style.visibility = visstring;
// adiv.innerHTML = msg;
}
和导出文件下载点击:
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + OUTPUTFILE + ".xls");
Response.AddHeader("Content-Type", "application/Excel");
Response.ContentType = "application/ms-excel.xls";
Response.AddHeader("Content-Length", file_New.Length.ToString());
Response.WriteFile(file_New.FullName);
Response.Flush();
file_New.Delete();
答案 0 :(得分:4)
因为响应在文件发送到客户端时结束。 你需要在新的页面中做到这一点。
在您的页面中,将代码替换为:
Session["OUTPUTFILE"] = OUTPUTFILE;
ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "var Mleft = (screen.width/2)-(760/2);var Mtop = (screen.height/2)-(700/2);window.open( 'file.aspx', null, 'height=700,width=760,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,top=\'+Mtop+\', left=\'+Mleft+\'' );", true);
新页面(称为“file.aspx”):
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + Session["OUTPUTFILE"].ToString()+ ".xls");
Response.AddHeader("Content-Type", "application/Excel");
Response.ContentType = "application/ms-excel.xls";
Response.AddHeader("Content-Length", file_New.Length.ToString());
Response.WriteFile(file_New.FullName);
Response.Flush();
file_New.Delete();
}