顯示具有 下載 標籤的文章。 顯示所有文章
顯示具有 下載 標籤的文章。 顯示所有文章

2023年5月4日 星期四

V20302 - 下載文字檔案至前端

 目的:  V20302 - 下載文字檔案至前端

處理說明: 1>    必需用 form.submit , 不可用 Ext.Ajax
                  2>    standardSubmit: true, 必用設為 true



1>1>*.js 

//  //[確認除帳]鈕 -
    function DoRemoveBtn_click() {
        var cur_recs = Ext.getCmp('grid_Single').getSelectionModel().getSelection();
        if (cur_recs.length == 0) {
            mywarnalert("請先選擇要編輯的資料");
            return false;
        }
        var cur_rec;
        let Tmp_DataAry1 = [];
        var Tmp_RPNOAF;
        var Tmp_Str="";
        console.log("cur_recs.length:", cur_recs.length);        
        //判斷約別, 不可空白, 若有空白,則顯示訊息,不再往下
        for (let i = 0; i <= cur_recs.length - 1; i++) {
            console.log("i:", i);
            cur_rec = cur_recs[i];
            if (checkisnull(cur_rec.data["TYPEAF"])) {
                Tmp_Str = Tmp_Str + "系統件單號(" + cur_rec.data["FMNO"] + ")的約別不可空白<br>";
            }
        }  // for i=0 to 
        if (Tmp_Str.length > 0) {
            Tmp_Str = Tmp_Str 
                           + "請檢核  !!<br>";
            mywarnalert(Tmp_Str);
            return;
        }

        //判斷約別是否一致  &  執行結果是否有錯誤, 若有,則存成 *_log.txt 傳至前端
        //若有成功除帳的資料,則發  Email 通知使用者
        for (let i = 0; i <= cur_recs.length - 1; i++) {
            console.log("i:", i);
            cur_rec = cur_recs[i];
            Tmp_DataAry1.push(cur_rec.data);
        }  // for i=0 to 
        let Tmp_DataString1 = JSON.stringify(Tmp_DataAry1);
            var np = {};
        np["DataString1"] = Tmp_DataString1;            
            var isOk = true; 
         ///若下載檔案至前端, 則不可用 Ext.Ajax
        //Ext.Ajax.request({
            Ext.getCmp('s_form').submit({
                method: "POST",
                url: '../../api/V20302API/DoRemove',
                standardSubmit: true,      
                params: np,
                async: false,
                success: function (response, opts) {
                    console.log("0 response.responseText=", response.responseText);
                    var Tmp_Obj = Ext.decode(response.responseText);
                    console.log("1  Tmp_Obj=", Tmp_Obj);
                    if (Tmp_Obj["success"] == false) {
                        var Tmp_RtnMsg = "系統件除帳,錯誤訊息如下: <br>"
                            + Tmp_Obj["Rtn_Msg"];
                        mywarnalert(Tmp_RtnMsg);
                        isOk = false;
                    }
                    else {
                        Tmp_Str = "系統件除帳 OK <br>";                            
                        mywarnalert(Tmp_Str);
                    };

                    
                    
                },  //end of success                
                failure: function (response, opts) {
                    var Tmp_Obj = Ext.decode(response.responseText);
                    var Tmp_Rtn_Msg = "系統件除帳,錯誤訊息如下: <br>"
                        + Tmp_Obj["Rtn_Msg"];                        
                    mywarnalert(Tmp_Rtn_Msg);
                    isOk = false;
                }            
            })  //end of Ext.Ajax.Request
         };  // end of    function ConfirmRemoveBtn_click() {
2>
2>2>*.cs
pupublic void DoRemove()
        {
            var c = HttpContext.Current;
            NameValueCollection nvc = c.Request.Form;
            OracleConnection conn = new OracleConnection(DBService.ConnectionString(DBLINK));//
            OracleCommand cmd = new OracleCommand();
            OracleDataReader reader;
            var response = Request.CreateResponse();

            conn.Open();
            string Tmp_RtnMsg;
            try
            { 
            conn.ClientInfo = User.Identity.Name;
            conn.ModuleName = BaseSYS + "_" + BaseMODID;
            conn.ActionName = ActionName;
            cmd.Connection = conn;
            string Tmp_Sql = "";
            
            string Tmp_out_FName = "V20306_系統件除帳_log.txt";      //處理_log.txt
          string documentPath=HttpContext.Current.Server.MapPath("~") +"document\\"; //取得實實的路徑            
            string Tmp_out_pFName = documentPath + Tmp_out_FName;    //含 path 的Filename
            if (File.Exists(Tmp_out_pFName))
            {
                File.Delete(Tmp_out_pFName);
            }

            //將 table 字串轉成 DataTable
            //DataTable DataTable1 = (DataTable)JsonConvert.DeserializeObject(Tmp_DataString1, (typeof(DataTable)));
            string Tmp_DataString1 = nvc["DataString1"];    //Tmp_DataString:  將目前子畫面的資料. JSON字串                           
            DataTable dt1 = myfunc.JSON2dt(Tmp_DataString1);
                //每筆資料列印後, 不分頁列印下一筆            
                string Tmp_FMNO;
            string Tmp_Str, Tmp_RPNOAF, Tmp_CSM_TYPEAF, Tmp_FM_TYPEAF;
            //using (StreamWriter sw = new StreamWriter(Tmp_out_pFName))   //小寫TXT  
            //不用 sw, 改用File.AppendText(Tmp_out_pFName,Tmp_Str)  
            {
                for (var i = 0; i < dt1.Rows.Count; i++)
                {
                    Tmp_FMNO = dt1.Rows[i]["FMNO"].ToString();
            Tmp_Sql = " SELECT   A.RPNOAF,A.TYPEAF as CSM_TYPEAF,B.TYPEAF as FM_TYPEAF  "
                                    + "  FROM     CSM_AF@TLS_245 A, AMM_FM B   "
                                    + "   WHERE  A.RPNOAF = nvl(B.MRPNOAF, B.RPNOAF)  "
                                    + "   AND        B.FMNO = " + myfunc.AA(Tmp_FMNO);
                    Tmp_Str = myfunc.SqlValue(Tmp_Sql);
                    Tmp_RPNOAF = myfunc.StrExtract(Tmp_Str, 1);
                    Tmp_CSM_TYPEAF = myfunc.StrExtract(Tmp_Str, 2);
                    Tmp_FM_TYPEAF = myfunc.StrExtract(Tmp_Str, 3);
                    if (Tmp_CSM_TYPEAF != Tmp_FM_TYPEAF)
                    {
                        Tmp_Str = "交修單號(" + Tmp_RPNOAF + ")"
                                       + " AMM約別(" + Tmp_FM_TYPEAF + ") "
                                       + "和 TLS的約別(" + Tmp_CSM_TYPEAF + ") 不同 ,"
                                       + "敬請檢核!!<br>";
//                      //sw.WriteLine(Tmp_Str);
                        File.AppendAllText(Tmp_out_pFName, Tmp_Str);  //不用 sw , 改用 File.AppendAllText
                    }
                }//   for (var i = 0; i < dt1.Rows.Count; i++)
                // sw.Close才會實際寫入檔案(Tmp_out_pFName)
//              //sw.Close();
                    
                    //讀取檔案的文字內容 , 若內容>0 ,郥 success : false , 傳回檔案內容
                    //string Tmp_FStr = File.ReadAllText(Tmp_out_pFName);
                    //if  (Tmp_FStr.Length>0)
                    //{
                    //    Tmp_RtnMsg = "[確認除帳]失敗 !!  <br>"
                    //                     + Tmp_FStr;
                    //    response.Content = new StringContent("{success: false,Rtn_Msg:" + Tmp_RtnMsg+ "}");    // 回應內容
                    //}
                    //else
                    //    response.Content = new StringContent("{success: true,Rtn_Msg:約別正確}");    // 回應內容
                    //return response;
                    

                    ////FileInfo fileInfo = new FileInfo(Tmp_out_pFName);                    
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode((string)Tmp_out_FName, System.Text.Encoding.UTF8) + "\"");
                    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                    HttpContext.Current.Response.ContentType = "text/plain";                  
                    ////HttpContext.Current.Response.BinaryWrite(ms.ToArray());
                    HttpContext.Current.Response.WriteFile(Tmp_out_pFName);
                    //////包裝標籤檔產生完成
                    ////Tmp_Str = "產生包裝匯出檔案成功<br>"
                    ////                + "(" + FileName + ")<br>";
                    ////MyCookie = new HttpCookie("EX_DFile", HttpUtility.UrlEncode(Tmp_Str));
                    ////HttpContext.Current.Response.Cookies.Add(MyCookie);
                    HttpContext.Current.Response.End();                
            };// using 
        }  // end of try
            catch (Exception e)
            {
                string Tmp_msg;
                Tmp_msg = myfunc.Get1ORA(e.Message);
                Tmp_RtnMsg = "[確認除帳]失敗 !!  <br>"
                                         + Tmp_msg;
                //response.Content = new StringContent("{'success': false,'Rtn_Msg':'" + Tmp_RtnMsg + "'}");    // 回應內容
                //return response;
            }
            finally
            {
              conn.Close();
            }
        } // end of DoRemove


1


2022年7月7日 星期四

Web改版 V120902 1>xls檔案未下載至前端Browser 2>TMGrid_高度設定_分頁控制Bar置於下方 3>主機下載檔案目錄權限設定

 1>[列印點名表] – 檔案下載至前端  -  standardSubmit: true, 且 必需以  form.submit, 不可用 Ext.Ajax


1>*.js

Ext.getCmp('mySubForm').submit({   

                url: '../../api/V120902API/DoPRNLIST',

                method: 'POST',

//  Ajax 的方式  //若要傳送檔案至前端, standardSubmit必需設為 true , 才會開新視窗,傳送 Binary檔案

//否則 Binary 檔案會放在原本的  Response

                standardSubmit: true,      

                params: np,             

        });

//顯示處理中..請稍待的訊息

var mask = new Ext.LoadMask(Ext.getBody(), {

                msg: '處理中,請稍待...'

        }); 

        mask.show();//使用 mask 需手動呼叫show() 方法下

        var timer = setInterval(function () {

                clearInterval(timer);

                mask.hide();

                timer = null;

                var r = r_cookies('EX_DFile');

                mysuccessalert(r);

        },1000);  //1000ms = 3sec

};  // end of function mySub1_OkBtn_click()







2>[缺點現況維護] -  TMGrid 的頁次未置於最下方 - Panel2 自動充滿畫面   - page


//panel2 的高度必需設定, 否則  panel2的高度會隨 Grid而變化
win.on('resize', function (me, width, height, eOpts) {
            //console.log("win.resize height=",height);
            var Tmp_okpanel_Height = Ext.getCmp('mySub2_OkBtn').getHeight() + 20;            
            var Tmp_rHeight = Ext.getCmp('mySubForm').getHeight() - Ext.getCmp('sub2_panel1').getHeight() - Tmp_okpanel_Height;            
            Ext.getCmp('sub2_panel2').setHeight(Tmp_rHeight);
            //console.log("win.resize Tmp_rHeight=", Tmp_rHeight);            
        });

// [缺勤記錄維護] - 開啟子視窗, 所有的變數在 子視窗開啟時, 要重新宣告,

    //                            --> 因為關閉子視窗時, 變數記憶體會被收回, 不重新宣告變數, 該變數會不存在

    function RollEditBtn_click() {

        var checkbox_id = 1;

        //按鈕 : [確認] [取消]  - [缺勤記錄維護]

        var mySub2_Btns = [

            {

                xtype: 'button', text: '確定', id: 'mySub2_OkBtn',

                listeners: {

                    click: function () {

                        mysuccessalert("mysub2_確定 ");

                        mySub2_OkBtn_click();

                    }

                }

            },

        ];

 

        var sub2_Columns = [

            { header: "員工編號", dataIndex: "EMPLYID", width: 150, TMType: "string" },

            { header: "姓名", dataIndex: "EMPLYID_", width: 100, TMType: "string" },

            { header: "缺勤", dataIndex: "NWK", width: 50, TMType: "string" },

            { header: "備註", dataIndex: "NOTE", width: 200, TMType: "string" },

            {

                header: '缺勤1',

                dataIndex: 'NWK',

                xtype: 'checkcolumn',               

                width: 60,

                editor: {

                    xtype: 'checkbox',

                    cls: 'x-grid-checkheader-editor',

                    inputValue: 'Y',

                    uncheckedValue: 'N'

                },           

                renderer: function (value, metadata, record, rowIndex, colIndex, store) {

                    console.log("1 renderer value:", value);

                    var tempVal = '';

                    if ((value === 'Y') || (value === true)) { tempVal = 'checked'; }     

                    var Tmp_Str = "<input  type='checkbox'  name=" + DatetoStr(record.get('DT')) + "_" + record.get('DEP') + "_" + record.get('EMPLYID') + "   " + tempVal + " >";

                    console.log("1 renderer return Tmp_Str:", Tmp_Str);

                    checkbox_id = checkbox_id + 1;

                    return Tmp_Str;

                },                               

                listeners: {

                    beforecheckchange: function (me, rowIndex, checked, eOpts) {

                        mysuccessalert("2 befoercheckchange !!");                       

                        console.log("2 befoercheckchange checked 1:", checked);                    

                        if (checked) { checked = false;checkVal = 'N'; }

                        else { checked = true; checkVal = 'Y'; }

                        Ext.getCmp('sub2_Grid').store.getAt(rowIndex).set('NWK', checkVal);

                        console.log("2 befoercheckchange checked 2:", checked);                    

                        return true;

                    },

                    click: function () { mysuccessalert("1 click event !!") }, 

                    // cellclick                   

                    checkchange: function (me, rowIndex, checked, eOpts) {

                         mysuccessalert("3 checkchange !!");

                        console.log(" 3 checkchange  checked :", checked);                           

                        var checkVal = '';

                        if (checked) {

                            checkVal = 'Y';                           

                            console.log("3  checkchange 目前 checked= Y !!  checkVal:", checkVal );

                        }

                        else {

                            checkVal = 'N';

                            console.log(" 3 checkchange 目前 checked= N !!  checkVal:", checkVal);

                        }                       

                        Ext.getCmp('sub2_Grid').store.getAt(rowIndex).set('NWK', checkVal);

                        console.log(" 4 checkchange Ext.getCmp(sub2_Grid).store.getat(rowIndex)", Ext.getCmp('sub2_Grid').store.getAt(rowIndex));                           

                        }

                    }

                },                               

        ];

 

 

        // [缺勤記錄維護]  - 子畫面欄位

        var mySub2 = [

            {

 type:'panel', bodyStyle: "background-color:transparent;", border: false, layout: { type: 'vbox', align: 'stretch' }, padding: "5",

                items: [

                    {

                        xtype: 'panel',

                        id: 'sub2_panel1',

                        layout: { type: 'hbox', align: 'stretch' },

                        flex: 5,

                        border: 1,

                        items: [

                        ]

                    },  //end of panel1                   

                    {

                        xtype: 'panel',

                        id: 'sub2_panel2',

                        //title: 'sub2_panel2',

                        layout: { type: 'vbox', align: 'stretch' },

                        //layout: 'fit',

                        flex: 10,

                        border: 1,                                               

                        items: [ sub2_Grid ]

                    },

                ] // end of   layout: "vbox", padding: "5", items: [

            }  //end of  my_Sub2 , items[{

        ]      //end of  my_Sub2 , items[

        var win = getMyWindow("缺勤記錄維護", mySub2, mySub2_Btns);       

        //Ext.getCmp('mySubForm').autoScroll = true;       

       

            var Tmp_Sql = " SELECT  EMPLYNM    "

                + "  FROM   HR_EMPLYM  "

                + " WHERE  EMPLYID=" + AA(Ext.getCmp('sub2_LDMAN').getValue());

            var Tmp_Str = SqlValue(Tmp_Sql);

            Ext.getCmp('sub2_LDMAN_').setValue(Tmp_Str);

        };

        //console.log("2  win: ", win);       

        win.width = 700;

        win.height = 400;

        win.show();

//Runtime 設定 Panel2 :  items[Sub2_Grid] 的高度, 以便充滿整個畫面

var Tmp_okpanel_Height = Ext.getCmp('mySub2_OkBtn').getHeight()+20;

var Tmp_rHeight =Ext.getCmp('mySubForm').getHeight()-Ext.getCmp('sub2_panel1').getHeight()-Tmp_okpanel_Height;

        Ext.getCmp('sub2_panel2').setHeight(Tmp_rHeight);

    };


3>V120902 – 在主機端無法正常下載檔案



Ans:    發佈至主機時,  web.config  檔案必需設定   user/password , 以便存取主機端的目錄
                    若沒設定,則無法存取主機端的目錄的權限,以致無法下載SS_FILES至主機目錄
1>web.config
 <system.web>
    <!--server端帳密,發行時請取消註解-->
    <!-- identity impersonate="true" userName="webadmin" password="Aidc@856+123" /-->
  -->  發佈至測試區/正式區時,  如上web.config 檔案   必需  remark 
<identity impersonate="true" userName="webadmin" password="Aidc@856+123" />