I have functionality to download excel file in my project. On clicking export button porgress bar is displayed.but even if browser's save as dialouge is appeared progress bar get not invisible. Problem is after response.end progress bar not used. Progress bar visible at asp.net ajax begin request and invisible at end request .code shown below.
Visible and hide progress bar:
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;
}
and file download on export click:
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();
because the response is ending when the file is sent to the client. you need to do it in new page.
in your page, replace your code in:
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);
the new page (called "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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With