我在
loader
上调用 OnClientClick
并在下载过程中加载它。但是,当我尝试在进程完成后隐藏加载程序时,它不起作用。加载器持续显示并加载。
这是代码。
function showloadingGif_UPLOAD() {
document.getElementById('ContentPlaceHolder1_divShowLoadingGif').style.display = 'inline';
return true;
}
function HideLoader() {
document.getElementById('ContentPlaceHolder1_divShowLoadingGif').style.display = 'none';
}
--loader div
<div id="ContentPlaceHolder1_divShowLoadingGif" class="dvLoader" style="display: none;">
<img id="img2" alt="" src="images/1487.png" />
</div>
-- button click
<input type="submit" name="ctl00$ContentPlaceHolder1$btnDownloadInfo" value="Report Download" onclick="showloadingGif_UPLOAD();" id="ContentPlaceHolder1_btnDownloadInfo" class="btn btn-primary downnloadReport" />
下面是调用隐藏函数的服务器端代码。
protected void btnDownloadInfo_Click(object sender, EventArgs e)
{
DataTable dtExcelData = new DataTable();
try
{
CommonUser ObjUser = new CommonUser();
string strDateFilter = txtDateSelection.Value;
dtExcelData = ObjUser.GET_EXCEL_REPORT(strDateFilter);
CommonDB.WriteLog("Dt Count 1 : " + dtExcelData.Rows.Count, ConfigurationManager.AppSettings["AIRFIBER_LOG"].ToString());
if (dtExcelData != null && dtExcelData.Rows.Count > 0)
{
CommonDB.WriteLog("Dt Count 2 : " + dtExcelData.Rows.Count, ConfigurationManager.AppSettings["AIRFIBER_LOG"].ToString());
DownloadReport(dtExcelData);
}
else
{
ScriptManager.RegisterStartupScript(Page, GetType(), "disp_confirm", "<script>HideLoader()</script>", false);
CommonDB.WriteLog("Dt Count 3 : " + dtExcelData.Rows.Count, ConfigurationManager.AppSettings["AIRFIBER_LOG"].ToString());
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('No record found');", true);
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(Page, GetType(), "disp_confirm", "<script>HideLoader()</script>", false);
string strErrorMsg = ex.Message.ToString() + " " + "StackTrace :" + ex.StackTrace.ToString();
CommonDB.WriteLog("ERROR:" + strErrorMsg, ConfigurationManager.AppSettings["AIRFIBER_LOG"].ToString());
}
}
public static void DownloadReport(DataTable dtRecord)
{
try
{
string strFilename = string.Empty;
using (XLWorkbook wb = new XLWorkbook())
{
CommonDB.WriteLog("Dt Count 3 : " + dtRecord.Rows.Count, ConfigurationManager.AppSettings["AIRFIBER_LOG"].ToString());
wb.Worksheets.Add(dtRecord, "SheetName");
strFilename = DateTime.Now.ToString();
CommonDB.WriteLog("Dt Count 4 : " + dtRecord.Rows.Count, ConfigurationManager.AppSettings["AIRFIBER_LOG"].ToString());
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=JIO_LOS_REPORT_"+ strFilename +".xlsx");
CommonDB.WriteLog("Dt Count 5 : " + dtRecord.Rows.Count, ConfigurationManager.AppSettings["AIRFIBER_LOG"].ToString());
using (MemoryStream MyMemoryStream = new MemoryStream())
{
CommonDB.WriteLog("Dt Count 6 : " + dtRecord.Rows.Count, ConfigurationManager.AppSettings["AIRFIBER_LOG"].ToString());
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(HttpContext.Current.Response.OutputStream);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
CommonDB.WriteLog("Dt Count 7 : " + dtRecord.Rows.Count, ConfigurationManager.AppSettings["AIRFIBER_LOG"].ToString());
}
}
}
catch (Exception ex)
{
string strErrorMsg = ex.Message.ToString() + " " + "StackTrace :" + ex.StackTrace.ToString();
CommonDB.WriteLog("ERROR:" + strErrorMsg, ConfigurationManager.AppSettings["AIRFIBER_LOG"].ToString());
}
}
这不是一个简单的解决方案......
常规回发请求(单击按钮) - 应该以新的整个页面渲染结果结束(+由名为 Response.Write + Response.End 的框架);
文件下载回发请求 - 还应以响应标头 =“attachmnent”结束(+通过手动调用 Response.BinaryWrite + Response.End); 等等。
实际上,
HttpContext.Current.Response.End();
之后的所有内容对客户端/浏览器都没有影响。这就是为什么所有可能的 ScriptManager.RegisterStartupScript
调用都不属于响应的一部分。因此,预定的 JavaScript 代码不会被评估/执行。当然,您可以尝试在代码中省略此调用,但有必要关心可能的副作用(以模拟“本机”Response.End)。
无论您在服务器端以何种方式创建附件,都需要正确(同步)下载它。
这里的主要挑战是告知最终用户(可能的)长时间(同步)操作。您可能会在不同的网络服务上看到不同类型的弹出窗口或新打开的页面,其中包含“...您的下载即将开始...”UI。
如果谈论你的代码,这个解决方案可以像这样实现:
function showloadingGif_UPLOAD() {
document.getElementById('ContentPlaceHolder1_divShowLoadingGif').style.display = 'inline';
setTimeout(HideLoader, DESIRED_MINIMAL_TIMEOUT);
return true;
}
function HideLoader() {
document.getElementById('ContentPlaceHolder1_divShowLoadingGif').style.display = 'none';
}