2

我有在我的项目中下载 excel 文件的功能。单击导出按钮时,显示进度条。但即使出现浏览器的另存为对话框,进度条也不会不可见。问题是在 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();
4

1 回答 1

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();
}
于 2013-06-10T11:11:24.660 回答