2024年11月12日 星期二

V20304D – 傳送檔案至前端 - 下載 – 檔案名稱

 目的: V20304D – 傳送檔案至前端 -  下載 檔案名稱

處理說明:    
               1>    //傳送檔案至前端的檔案名稱(不含path)
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode((string)Tmp_out_FName, System.Text.Encoding.UTF8) + "\"");

             2>傳送實體檔案內容(含path)
                    HttpContext.Current.Response.WriteFile(Tmp_out_pFName);  //實體檔案內容(含path)


1>*.cs
   if (!is_Ok)
                {
                    //訊息加入 Cookie
                    Tmp_Str = "[確認轉TLS]執行失敗!!<br>"
                                    +"請參閱附件檔("+ Tmp_out_FName+")";
                    MyCookie = new HttpCookie("V20304D", HttpUtility.UrlEncode(Tmp_Str));
                    HttpContext.Current.Response.Cookies.Add(MyCookie);
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                    //傳送檔案至前端的檔案名稱(不含path)
                    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.WriteFile(Tmp_out_pFName);  //實體檔案內容(含path)
                    HttpContext.Current.Response.End();
                    return;
                }

2024年11月7日 星期四

V80403C1 – 顯示資料庫的照片- Ext.img – FILEBODY - JPG

 目的:V80403C1 – 顯示資料庫的照片- Ext.img – FILEBODY - JPG

處理說明: 
1>建立 Ext.img 元件
    var cur_Image = Ext.create('Ext.Img', {
        id: 'sub_V80403C1_img1',        
     });

var sub_V80403C1_Flds = [
        {
            type: 'panel', bodyStyle: "background-color:transparent;", border: 5, padding: "1",
            layout: 'border',
            items: [
                {
                    xtype: 'panel',
                    id: 'sub_V80403C1_panel1',
                    region: 'center',
                    layout: 'fit',
                    flex: 5,
                    border: 1,
                    items: [sub_V80403C1_Grid]
                },
                {
                    xtype: 'panel',
                    id: 'sub_V80403C1_panel2',
                    region: 'south',
                    height: 400,
                    layout: 'fit',
                    flex: 5,
                    border: 1,
                    items: [cur_Image]
                },
            ],
        },
    ]; // end of   layout: "vbox", padding: "5", items: [


1>  取得照片檔 (byte[]) - 並傳至 response
var Tmp_url = "../api/myAPI/downloadFile2?aTbl=" + Tmp_TblNM + "&Flds=" + Tmp_Flds + "&aCnd=" + Tmp_Cnd;
            Ext.getCmp('sub_V80403C1_img1').setSrc(Tmp_url);



1>*.js
var Tmp_url = "../api/myAPI/downloadFile2?aTbl=" + Tmp_TblNM + "&Flds=" + Tmp_Flds + "&aCnd=" + Tmp_Cnd;
 // 接收方式:
   //1>下載檔案  : document.location = Tmp_url;
   //2>顯示照片檔 : Ext.getCmp('sub_V80403C1_img1').setSrc(Tmp_url);

var cur_Image = Ext.create('Ext.Img', {

        id: 'sub_V80403C1_img1',        
    });

    var sub_V80403C1_Flds = [
        {
            type: 'panel', bodyStyle: "background-color:transparent;", border: 5, padding: "1",
            layout: 'border',
            items: [
                {
                    xtype: 'panel',
                    id: 'sub_V80403C1_panel1',
                    region: 'center',
                    layout: 'fit',
                    flex: 5,
                    border: 1,
                    items: [sub_V80403C1_Grid]
                },
                {
                    xtype: 'panel',
                    id: 'sub_V80403C1_panel2',
                    region: 'south',
                    height: 400,
                    layout: 'fit',
                    flex: 5,
                    border: 1,
                    items: [cur_Image]
                },
            ],
        },
    ]; // end of   layout: "vbox", padding: "5", items: [


Ext.getCmp("sub_V80403C1_Grid").on('selectionchange', function (me, eOpts) {
        V80403C1_OK1();  //顯示[不符照片]
    });

    // change the src of the image programmatically
    //[不符照片附檔瀏覽].[確認]鈕 
    function V80403C1_OK1() {
        var cur_recs = Ext.getCmp('sub_V80403C1_Grid').getSelectionModel().getSelection();
        if (cur_recs.length == 0) {
            //mywarnalert("請先選擇資料");
            return;
        }
        var cur_rec = cur_recs[0];
        var Tmp_FMNO = nulltoStr(cur_rec.data["FMNO"]).toString();
        var Tmp_ITMPH = nulltoStr(cur_rec.data["ITMPH"]).toString();
        var Tmp_FILENAME = nulltoStr(cur_rec.data["FILENAME"]).toString();
        var Tmp_TblNM;
        var Tmp_Flds;
        var Tmp_Cnd;
        //若 FILENO 無值,則瀏覽 AMM_OSMF
        Tmp_TblNM = "AMM_PH";
        Tmp_Flds = "FILENAME,FILEBODY";
        Tmp_Cnd = "  AND       FMNO=" + AA(cur_rec.data["FMNO"])
                          + "  AND       ITMPH=" + AA(cur_rec.data["ITMPH"]);
        try {
var Tmp_url = "../api/myAPI/downloadFile2?aTbl=" + Tmp_TblNM + "&Flds=" + Tmp_Flds + "&aCnd=" + Tmp_Cnd;
            Ext.getCmp('sub_V80403C1_img1').setSrc(Tmp_url);
        }
        catch (e) {
            var Tmp_Str = "不符照片處理失敗!!<br>"
                + "FM單號(" + cur_rec.data["FMNO"] + ")<br>"
                + "檔案名稱(" + cur_rec.data["Tmp_FILENAME"] + ")<br>"
                + "錯誤訊息:" + e.message;
            mywarnalert(Tmp_Str);
        }   //檔案無法讀取,仍會下載 byte=0
    };  //V80403C1_OK1() {



2>*.cs
        //myAPIController.cs 必需先設定  DBLINK 
        public myAPIController()
        {
            //DBTable = "";            
            DBLINK = "AMMEU";
        }

//取得  Table 的 FILEBODY 欄位值  - 不含 SQL
        [HttpGet]
        public void downloadFile2(string aTbl, string Flds, string aCnd)
        {
            string[] Ary = Flds.Split(',');
            string fnm = Ary[0];   //檔案名 稱
            string fbdy = Ary[1];  //檔案內容

            string cnd = Repltag_vch(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
                }
                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
            { }
        }
    public byte[] downloadFile(string sql, string fileName, string fileBody,bool inline=false)
        {
            setActionName();
            OracleConnection conn = new OracleConnection(DBService.ConnectionString(DBLINK));
            conn.Open();
            conn.ClientInfo = User.Identity.Name;
            conn.ModuleName = BaseSYS + "_" + BaseMODID;
            conn.ActionName = ActionName;
            OracleCommand cmd = new OracleCommand(sql, conn);
            OracleDataReader reader = cmd.ExecuteReader();
            byte[] file = null;
            if (reader.Read())
            {
                try
                {
                    //clearing the content
                    HttpContext.Current.Response.ClearContent();

                    if (inline)
                    {
                        //直接在網頁上開啟PDF,需搭配前端用submit方式
                        HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=\"" + Uri.EscapeDataString((string)reader[fileName]) + "\"");
                        HttpContext.Current.Response.ContentType = "Application/PDF";                        
                    }
                    else
                    {
                        // adding the headers to file stream
                        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + Uri.EscapeDataString((string)reader[fileName]) + "\""); //Uri.EscapeDataString才能處理空白字元
                        //HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode((string)reader[fileName], System.Text.Encoding.UTF8) + "\"");
                    }
                    
                    //BinaryWriter bw = new BinaryWriter(HttpContext.Current.Response.OutputStream);
                    // converting data into bytes
                    file = (byte[])reader[fileBody];
                    //bw.Write(file);
                    //bw.Close();
                    HttpContext.Current.Response.BinaryWrite(file);  //將檔案內容傳回 response
                    HttpContext.Current.Response.End();
                }
                finally
                {
                    reader.Close();
                    conn.Close();
                }
            }
            return file;
        }




V80403C1 - 日期格式處理

 目的: V80403C1 - 日期格式處理

處理說明:  1>var sub_model = [{name: "FILEDATE",} ]
                      var sub_Columns = [
{ header: "附檔上載日期", dataIndex: "FILEDATE", width: 100, sortable: false, 
TMType: "date", renderer: Ext.util.Format.dateRenderer('Y/m/d') },
},

1>*.js
// [物料]頁 - Grid子畫面欄位
var sub_model = [
{ name: "FILENAME" },
{
name: "FILEDATE",
//convert: function (value, record) { return Ext.Date.format(value, 'Y/m/d'); }  //--> 不可加本行
},
{ name: "FILESIZE" },
];
var sub_Columns = [

//AMM_PH
{ header: "", xtype: "rownumberer", width: 40, align: "center", sortable: false },
{ header: "附檔名稱", dataIndex: "FILENAME", width: 200, sortable: false, TMType: "string" },
{ header: "附檔上載日期", dataIndex: "FILEDATE", width: 100, sortable: false, 
TMType: "date", renderer: Ext.util.Format.dateRenderer('Y/m/d') },
{ header: "附檔大小", dataIndex: "FILESIZE", width: 60, sortable: false, align: "right", TMType: "int" },
];






    

2024年11月6日 星期三

V80403C1 – Grid DoubleClick 產生子畫面

 目的: V80403C1 – Grid DoubleClick 產生子畫面

處理說明: 1> itemdblclick event
  Ext.getCmp('sub_V80403C_Grid').on("itemdblclick", function (me, record, item, index, e, eOpts) {

--> 如何偵測到那一Column 被 DoubleClick


1>*.js
//Grid DoubleClick event
//itemdblclick( this, record, item, index, e, eOpts )
Ext.getCmp('sub_V80403C_Grid').on("itemdblclick", function (me, record, item, index, e, eOpts) {
     myalert("Grid Column DoubleClick !! ");
     console.log("Grid Column DoubleClick !! ");
     var Tmp_FMNO = record.data["FMNO"];      
     CALL_V80403C1(Tmp_FMNO);
}



);

2024年11月4日 星期一

Local和測試區主機 - 執行結果不同 - 因電腦日期格式設定不同 - insert 資料庫日期欄位時,產生錯誤 - 非數值字元

 目的 - Local和測試區主機 - 執行結果不同,因為電腦日期格式設定不同

處理說明: 1>Tmp_RRNRAR='2008/10/10 上午 12:00:00'; //實際拆檢完成日期 
                       --> 由資料庫讀取的日期格式含 上午,
                       -->應將日期格式化成 ' 2008/10/10'
             --> Tmp_RRNRAR = myfunc.Date2Str(sheet1.Range[myfunc.GetExcelPos(8,i+1)].Value);  
                   Tmp_RRNRAR = myfunc.Date2Str(Tmp_dt.Rows[j]["RRNRAR"].ToString()) ;                   


1>*.cs

  for (int j = 0; j < Tmp_cnt; j++)
                    {
                        //+ "    B.IRESAK,B.NOTEAR,B.QDEP,B.PRNRAR,B.PMHRAR,B.RRNRAR  "                                  Tmp_PRNRAR = Tmp_dt.Rows[j]["PRNRAR"].ToString();
                        Tmp_PMHRAR = Tmp_dt.Rows[j]["PMHRAR"].ToString();
                        Tmp_RRNRAR = myfunc.Date2Str(Tmp_dt.Rows[j]["RRNRAR"].ToString()) ;


--> SQL : 
                INSERT INTO AMM_TLSAR
                (FMNO,RRNRAR,..)
                VALUES
                ('F202410050002,'2024/10/15 上午 10:15',)