2015-10-06 108 views
0

我有使用此我試圖下載使用的ReportViewer一個excel文件按鈕單擊事件jQuery函數後面在asp.net工作不

protected void btndwnReport_Click(object sender, EventArgs e) 
    { 
     try 
     {     
      save("Report1"); 
     } 
     catch (Exception ex) 
     { 
      Log.Errlog("Error Occured in btndwnReport_Clickof UI_Report Page : " + ex.Message.ToString()); 
     } 
     finally 
     {     
      Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "HideLoading", "HideLoading2();", true);//this function is not triggering 
     } 
    } 

。在點擊這個按鈕時,我顯示一個加載圖標(通過調用ShowLoading2()),它被定義爲一個jquery函數。

function ShowLoading2() { 
      try {  
       if (parent.document.getElementById('dvProgress')) 
        $("#dvProgress", parent.document).hide(); 
      } catch (e) { } 

      $("#dvProgress").show();    

     } 
function HideLoading2() { 
      try { 
       if (parent.document.getElementById('dvProgress')) 
        $("#dvProgress", parent.document).hide(); 
      } catch (e) { } 
      $("#dvProgress").show(); 
      $("#dvProgress").fadeOut(15000); 

     } 

我能下載Excel格式的報告,但不能調用HideLoading2()的代碼功能下載Excel廣告後落後。

保存時(「Report1」);方法被評論,能夠調用HideLoading2()。

這裏是保存方法

public void save(string ReportName) 
    { 
     Warning[] warnings; 
     string[] streamids; 
     string mimeType, encoding, extension, deviceInfo; 
     string format = "Excel"; byte[] bytes = null; 
     deviceInfo = "True";  

     bytes = rptViewer.ServerReport.Render("EXCEL", null, out mimeType, out encoding, out extension, out streamids, out warnings); 

     Response.Buffer = false; //transmitfile self buffers 
     Response.Clear(); 
     Response.ClearContent(); 
     Response.ClearHeaders(); 
     Response.ContentType = "GetReport/excel";    
     Response.AddHeader("Content-Disposition", "attachment; filename=" + ReportName + ".xls"); 
     Response.OutputStream.Write(bytes, 0, bytes.Length); 
     Response.OutputStream.Flush(); 
     Response.OutputStream.Close(); 
     Response.Flush(); 
     Response.Close(); 
    } 

我如何可以調用下載Excel工作表後HideLoading2()函數?

注意:我沒有在頁面中使用scriptmanager \ updatepanel。

+0

您是否看過您的按鈕回發後的標記,看看JavaScript是否寫入您的頁面? –

回答

1

一旦您調用了在文件下載時需要的Response.Flush()和Response.Close(),服務器將停止處理並返回響應。之後,您無法執行代碼。

通常爲了下載文件,請嘗試通過JavaScript異步調用函數並使用window.open打開文件。

+0

你可以給一些代碼示例嗎? – Athul

+1

@Athul請參閱此鏈接 - http://stackoverflow.com/a/15961851/5188835 您可以添加一個查詢字符串,並在Page_Load中檢查它,如果存在,則在服務器端調用所需的函數並返回響應。 –