讀寫大文本為防止注入等各種問題,將文本轉換為Unicode或UTF8進行保存
/// <summary>
/// 將文本字符串轉換成帶","號分離的二進制字符串
/// </summary>
/// <param name="strContent">文本字符串</param>
/// <returns>帶,號分離的二進制字符串</returns>
private string strTextTostrBin(string strText)
{
byte[] bytearr=null;
string stringtobin="";
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
bytearr=encoding.GetBytes(strText);
for(int i=0;i<bytearr.Length;i++)
{
stringtobin+=","+bytearr[i].ToString();
}
return stringtobin.Substring(1);
}
/// <summary>
/// 將帶,號分離的二進制字符串轉換成文本字符串
/// </summary>
/// <param name="strBin">帶,號分離的二進制字符串</param>
/// <returns>文本字符串</returns>
private string strBinTostrText(string strBin)
{
string [] bintostr=strBin.Split(',');
Array binArray=Array.CreateInstance(Type.GetType("System.Byte"),bintostr.Length);
for(int i=binArray.GetLowerBound(0);i<=binArray.GetUpperBound(0);i++)
{
binArray.SetValue(byte.Parse(bintostr[i]+""),i);
}
byte[] strtobin=new byte[bintostr.Length];
for(int i=binArray.GetLowerBound(0);i<=binArray.GetUpperBound(0);i++)
{
strtobin[i]=(byte)binArray.GetValue(i);
}
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(strtobin);
}
C#如何存取圖像
圖像文件的寫入,使用文件上傳控件將數據讀入流中,再寫入二字節數組中,直接寫入數據庫。
private void ImgDataReadWrite()
{
HttpPostedFile UpFile = UP_File.PostedFile;//HttpPostedFile對象,用於讀取圖象文件屬性
FileLength = UpFile.ContentLength;
try
{
if (FileLength == 0)
{
lblMessage.Text = "<b>請選擇您要上傳的文件</b>";
}
else
{
Byte[] FileByteArray = new byte[FileLength]; //圖象文件臨時儲存Byte數組
Stream StreamObj = UpFile.InputStream;//建立數據流對像
//或Stream myStream = openFileDialog1.OpenFile();
//讀取圖象文件數據,FileByteArray為數據儲存體,0為數據指針位置、FileLnegth為數據長度
StreamObj.Read(FileByteArray, 0, FileLength);
SqlConnection Con = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
String SqlCmd = "INSERT INTO ImageStore (ImageData,ImageContentType,ImageDescription,ImageSize) VALUES (@Image,@ContentType,@ImageDescription,@ImageSize)";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@Image", SqlDbType.Binary, FileLength).Value = FileByteArray;
CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = UpFile.ContentType;//記錄文件類型
//把其它單表數據記錄上傳
CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDescription.Text;
//記錄文件長度,讀取時使用
CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = UpFile.ContentLength;
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();
lblMessage.Text = "<p><b>OK!你已經成功上傳你的圖片</b>";//提示上傳成 功
}
}
catch (Exception ex)
{
lblMessage.Text = ex.Message.ToString();
}
}
圖像文件的讀取,直接寫入流
private void ImgDataRead()
{
int ImgID = Convert.ToInt32(Request.QueryString["id"]);
SqlConnection Con = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
String SqlCmd = "SELECT * FROM ImageStore WHERE ID = @ImageID";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@ImageID", SqlDbType.Int).Value = ImgID;
Con.Open();
SqlDataReader SqlReader = CmdObj.ExecuteReader();
SqlReader.Read();
Response.ContentType = (string)SqlReader["ImageContentType"];//設定輸出文件類型
//輸出圖象文件二進制數制
Response.OutputStream.Write((byte[])SqlReader["ImageData"],0,Convert.ToInt32(SqlReader ["ImageSize"]));
Response.BufferOutput = true;
//或 byte[] bytes= (byte[])SqlReader["ImageData"];
// MemoryStream memStream=new MemoryStream(bytes);
// try
// {
// Bitmap myImage = new Bitmap(memStream);
// this.pictureBox1.Image= myImage;
// }
// catch
// {
// this.pictureBox1.Image=null;
// }
Con.Close();
}