2009年4月2日 星期四

將圖或FLASH檔存入資料庫,並且顯示在網頁上的用法

建立一個FileUpload物件,命名為FileUp

儲檔CS:
==================================================
int imgLen = FileUp.PostedFile.ContentLength;
byte[] imgBinaryData = new byte[imgLen];
imgBinaryData = FileUp.FileBytes;
SqlConnection usrcn = new SqlConnection();
usrcn.ConnectionString = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
SqlCommand cmd_file = new SqlCommand();
cmd_file.Connection = usrcn;
cmd_file.CommandText = string.Format(@"Update ImageTB set Image=@image where imgid={0}", imgid);
cmd_file.CommandType = CommandType.Text;
//欄位型態必為Image
cmd_file.Parameters.Add("@image", SqlDbType.Image).Value = imgBinaryData;
usrcn.Open();
cmd_file.ExecuteNonQuery();
usrcn.Close();
==================================================

顯示:

ps1:無論哪個設定,請務必設定寬高,使得圖檔大小一致化
ps2:ImageHandler.ashx用法請參考下列網址!
ps3:ImageHandler.ashx若要帶參數,如同一般網頁設定方式即可!取值方式:context.Request["type"].ToString()

參考網址:
http://blog.xuite.net/alwaysfuturevision/liminzhang/9538604
http://www.blueshop.com.tw/board/show.asp?subcde=BRD20061012135834YHQ&fumcde=FUM20041006161839LRJ&rplcnt=29
http://www.blueshop.com.tw/board/show.asp?subcde=BRD2008061010385466M&fumcde=FUM20050124192253INM

感謝Polly協助