此方法適合ASP.NET(C#)+Oracle
Oracle 表中欄位類型為Blob
#region 上傳檔案(二進位)
//
static public Byte[] FileToBinary(FileUpload hifile)
{
try
{
if (hifile.HasFile)
{
if (IsAllowedExtension(hifile))
{
//取得上傳檔案的大小
int FileLen = hifile.PostedFile.ContentLength;
Byte[] FileData = new Byte[FileLen];
//建立訪問用戶端上傳檔案的對象
HttpPostedFile hp = hifile.PostedFile;
//建立資料流對象
Stream sr = hp.InputStream;
//將圖片資料放到FileData數組對象執行個體中,0代表數組指標的起始位置,FileLen代表指標的結束位置
sr.Read(FileData, 0, FileLen);
//string id = "0";
//BLL.Picture.AddPictures(id,"name",Convert.ToString(FileData),"url","說明");
return FileData;//返回二進位檔案流 只要把它存入資料庫即可
}
else
{
//Response.Write("<script languge = ‘javascript‘ type = ‘text/javascript‘>alert(‘上傳檔案格式不對,只允許上傳jpg和gif格式的檔案‘);</script>");
return null;// "上傳檔案格式不對,只允許上傳jpg和gif格式的檔案";
}
}
else
{
return null;// "請選擇上傳檔案";
//Response.Write("<script language = ‘javascript‘ type = ‘text/javascript‘>alert(‘請選擇上傳檔案‘);</script>");
}
}
catch
{
return null;
}
}
#endregion