2025年3月26日 星期三

V20305I –[附檔瀏覽] – 下載檔案 - downloadFile2 - 子畫面- 簡單 Grid

 目的: V20305I –[附檔瀏覽] – 下載檔案

處理說明: 1>呼叫   downloadFile2
 var Tmp_url = "../api/myAPI/downloadFile2?aTbl=" + Tmp_TblNM + "&Flds=" + Tmp_Flds + "&aCnd=" + Tmp_Cnd;


1>*.js
    var Tmp_url = "../api/myAPI/downloadFile2?aTbl=" + Tmp_TblNM + "&Flds=" + Tmp_Flds + "&aCnd=" + Tmp_Cnd;       document.location = Tmp_url;     

2>*.cs

        public myAPIController()
        {
            //DBTable = "";            
            DBLINK = "AMMEU";
        }   

 [HttpGet]
        public void downloadFile2(string aTbl, string Flds, string aCnd)
        {
            string[] Ary = Flds.Split(',');
            string fnm = Ary[0];   //檔案名 稱
            string fbdy = Ary[1];  //檔案內容
            
            string cnd = aCnd;
            string Strsql = "select " + Flds + "  from " + aTbl + " where 1=1 " + cnd;
            string Tmp_CntSql = "select count(*)  from " + aTbl + " where 1=1 " + cnd;
            string Tmp_Str;
            int Tmp_cnt;
            HttpCookie MyCookie;
            try
            {
                Tmp_cnt = int.Parse(myfunc.SqlValue(Tmp_CntSql));
                if (Tmp_cnt >= 0)
                {
                    //必需先加入 Cookie , 再 downloadFile 
                    //--> 因 downloadFile 會 HttpContext.Current.Response.End(); , 所以 要先加 Cookie , 再 downloadFile
                    Tmp_Str = "檔案下載完成!!<br>";
                    MyCookie = new HttpCookie("Rtn_Msg", HttpUtility.UrlEncode(Tmp_Str));
                    HttpContext.Current.Response.Cookies.Add(MyCookie);
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                    downloadFile(Strsql, fnm, fbdy); //將 filebody 欄位以 byte[] 傳至 response 
                    // 接收方式:
                    //1>下載檔案  : document.location = Tmp_url;
                    //2>顯示照片檔 : Ext.getCmp('sub_V80403C1_img1').setSrc(Tmp_url);
                }
                else
                {
                    Tmp_Str = "檔案不存在,請檢核!!<br>";
                    MyCookie = new HttpCookie("Rtn_Msg", HttpUtility.UrlEncode(Tmp_Str));
                    HttpContext.Current.Response.Cookies.Add(MyCookie);
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                }
            }
            catch (Exception ex)
            {
                Tmp_Str = ex.Message;
                if (Tmp_Str.IndexOf("DBNull") > -1)
                    Tmp_Str = "檔案內容為空白,敬請檢核!!<br>";
                MyCookie = new HttpCookie("Rtn_Msg", HttpUtility.UrlEncode(Tmp_Str));
                HttpContext.Current.Response.Cookies.Add(MyCookie);
                HttpContext.Current.ApplicationInstance.CompleteRequest();
                 throw ex; 
            }   //檔案無法讀取,仍會下載 byte=0
            finally
            { }
        }

        



沒有留言:

張貼留言