ASP.NET中常用功能代码总结(3)——上传图片到数据库
生活随笔
收集整理的這篇文章主要介紹了
ASP.NET中常用功能代码总结(3)——上传图片到数据库
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
ASP.NET中常用功能代碼總結(jié)(3)——上傳圖片到數(shù)據(jù)庫<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
整理:Terrylee
一.上傳圖片到數(shù)據(jù)庫
?1/**////?<summary>
?2///?上傳圖片
?3///?</summary>
?4private?void?UploadFile()
?5{
?6????/**////得到用戶要上傳的文件名
?7????string?strFilePathName?=?loFile.PostedFile.FileName;
?8????string?strFileName?=?Path.GetFileName(strFilePathName);
?9????int?FileLength?=?loFile.PostedFile.ContentLength;
10
11????if(FileLength<=0)
12????????return;
13
14????/**////上傳文件
15????try
16????{
17????????
18????????/**////圖象文件臨時(shí)儲(chǔ)存Byte數(shù)組
19????????Byte[]?FileByteArray?=?new?Byte[FileLength];
20
21????????/**////建立數(shù)據(jù)流對(duì)像
22????????Stream?StreamObject?=?loFile.PostedFile.InputStream;?
23
24????????/**////讀取圖象文件數(shù)據(jù),FileByteArray為數(shù)據(jù)儲(chǔ)存體,0為數(shù)據(jù)指針位置、FileLnegth為數(shù)據(jù)長(zhǎng)度
25????????StreamObject.Read(FileByteArray,0,FileLength);?
26
27????????/**////建立SQL?Server鏈接
28????????string?strCon?=?System.Configuration.ConfigurationSettings.AppSettings["DSN"];
29????????SqlConnection?Con?=?new?SqlConnection(strCon);
30????????String?SqlCmd?=?"INSERT?INTO?ImageStore?(ImageData,?ImageContentType,?ImageDescription,?ImageSize)?VALUES?(@Image,?@ContentType,?@ImageDescription,?@ImageSize)";
31????????SqlCommand?CmdObj?=?new?SqlCommand(SqlCmd,?Con);
32????????CmdObj.Parameters.Add("@Image",SqlDbType.Binary,?FileLength).Value?=?FileByteArray;
33????????CmdObj.Parameters.Add("@ContentType",?SqlDbType.VarChar,50).Value?=?loFile.PostedFile.ContentType;?//記錄文件類型
34????????
35????????/**////把其它單表數(shù)據(jù)記錄上傳
36????????CmdObj.Parameters.Add("@ImageDescription",?SqlDbType.VarChar,200).Value?=?tbDescription.Text;
37????????
38????????/**////記錄文件長(zhǎng)度,讀取時(shí)使用
39????????CmdObj.Parameters.Add("@ImageSize",?SqlDbType.BigInt,8).Value?=?FileLength;
40????????Con.Open();
41????????CmdObj.ExecuteNonQuery();?
42????????Con.Close();
43
44????????/**////跳轉(zhuǎn)頁面
45????????Response.Redirect("ShowAll.aspx");
46????}
47????catch(Exception?ex)
48????{
49????????throw?ex;
50????}
51}
二.從數(shù)據(jù)庫中讀取圖片
?1/**////?<summary>
?2///?顯示圖片
?3///?</summary>
?4private?void?ShowImages()
?5{
?6????/**////ID為圖片ID
?7????int?ImgID?=?Convert.ToInt32(Request.QueryString["ID"]);??
?8????
?9????/**////建立數(shù)據(jù)庫連接
10????string?strCon?=?System.Configuration.ConfigurationSettings.AppSettings["DSN"];
11????SqlConnection?Con?=?new?SqlConnection(strCon);
12????String?SqlCmd?=?"SELECT?*?FROM?ImageStore?WHERE?ImageID?=?@ImageID";
13????SqlCommand?CmdObj?=?new?SqlCommand(SqlCmd,?Con);
14????CmdObj.Parameters.Add("@ImageID",?SqlDbType.Int).Value?=?ImgID;
15????
16????Con.Open();
17????SqlDataReader?SqlReader?=?CmdObj.ExecuteReader();
18????SqlReader.Read();?
19
20????/**////設(shè)定輸出文件類型
21????Response.ContentType?=?(string)SqlReader["ImageContentType"];
22????
23????/**////輸出圖象文件二進(jìn)制數(shù)制
24????Response.OutputStream.Write((byte[])SqlReader["ImageData"],?0,?(int)SqlReader["ImageSize"]);?
25????Response.End();
26
27????Con.Close();????
28}
轉(zhuǎn)載于:https://www.cnblogs.com/Terrylee/archive/2005/11/30/287982.html
總結(jié)
以上是生活随笔為你收集整理的ASP.NET中常用功能代码总结(3)——上传图片到数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有关 alter tablespace
- 下一篇: WPF应用程序模型