Loader is not getting hide after download is completed in C# asp.net

I am calling loader on OnClientClick and its loading when the process of downloading in process. But when I try to hide the loader once the process gets completed, it doesn't works. The loader continously displays and loads.

Here is the code.

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" />

Also below is the server side code to call the hide function.

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());
        }
    }


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation