2026年5月27日 星期三

V1060302-實體定更定檢清單-匯出 –後端錯誤時,出現黑畫面 --> 不用 Response.End(); -->改用 CompleteRequest()

 目的: V1060302-實體定更定檢清單-匯出 –後端錯誤時,出現黑畫面 
           --> Exception :不用 Response.End();  -->改用 CompleteRequest()
           --> 寫入檔案  : 用 Response.End(); 不用 CompleteRequest();

處理說明:  1> catch (e:Message){                          
                        不可用 HttpContext.Current.Response.End();               
                        à改用HttpContext.Current.ApplicationInstance.CompleteRequest();
                       }
                 2> 寫入檔案時,要用 Response.End();
                       不可用 HttpContext.Current.ApplicationInstance.CompleteRequest();   
                      //檔案會無法開啟
                       
                HttpContext.Current.Response.Cookies.Add(MyCookie);
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(Tmp_FName1, System.Text.Encoding.UTF8) + "\"");
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                HttpContext.Current.Response.BinaryWrite(mstream.ToArray());
                HttpContext.Current.Response.End();
                HttpContext.Current.ApplicationInstance.CompleteRequest();   //檔案會無法開啟



1>*.cs
          try{
                wk.SaveToStream(mstream, FileFormat.Version2007);
                wk.SaveToFile(Tmp_pFName1, FileFormat.Version2007);
                Tmp_Str = "已匯出完成!!(" + row.ToString() + "筆)<br>"
                            + "檔案名稱(" + Tmp_FName1 + ")";
                MyCookie = new HttpCookie("Rtn_Msg", HttpUtility.UrlEncode(Tmp_Str));
                HttpContext.Current.Response.Cookies.Add(MyCookie);
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(Tmp_FName1, System.Text.Encoding.UTF8) + "\"");
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                HttpContext.Current.Response.BinaryWrite(mstream.ToArray());
                HttpContext.Current.Response.End();
                HttpContext.Current.ApplicationInstance.CompleteRequest();   //檔案會無法開啟

         
}         catch (Exception e)
            {
                //Console.WriteLine(e);
                Tmp_Str = "錯誤:匯出檔案失敗,訊息如下:<br>"
                               + e.Message;
                MyCookie = new HttpCookie("Rtn_Msg", HttpUtility.UrlEncode(Tmp_Str));
                HttpContext.Current.Response.Cookies.Add(MyCookie);
                //HttpContext.Current.Response.End();                
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }


2>*.js
 Ext.getCmp('s_form').submit({
        //Ext.Ajax.request({
        url: '../../api/V1060302CAPI/XLSOUT',
        method: 'POST',
        async: false,
        standardSubmit: true, //若要傳送檔案至前端, standardSubmit必需設為 true  
        params: np,
    });
    //顯示結果訊息..

    var mask = new Ext.LoadMask(Ext.getBody(), {
        msg: '處理中,請稍待...'
    });

    mask.show();//使用 mask 需手動呼叫show() 方法下
    //每1秒檢核一次,是否已完成, 若已完成,則不再檢核
    var timer = setInterval(function () {
        var r = r_cookies('Rtn_Msg');  //Exception 時, 若用 Response.End() , 則會抓不到 Cookie
        //console.log("r_cookies=", r);
        if (!checkisnull(r)) {
            mysuccessalert(r);
            clearInterval(timer);
            mask.hide();
            timer = null;
        }
    }, 1000);  //1000ms = 1sec

沒有留言:

張貼留言