目的: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);
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;
}
沒有留言:
張貼留言