把圖片儲存到sql server資料庫裡
來源:互聯網
上載者:User
先在SQL Server中建立一個圖片儲存的數庫表,ImageData Column為圖象位元據儲存欄位,ImageContentType Column為圖象檔案類型記錄欄位,ImageDescription Column為儲蓄圖象檔案說明欄位,ImageSize Column為儲存圖象檔案長度欄位,結構如下:
CREATE TABLE [dbo].[ImageStore] (
[ImageID] [int] IDENTITY (1, 1) NOT NULL ,
[ImageData] [image] NULL ,
[ImageContentType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[ImageDescription] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
[ImageSize] [int] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
*/
//UpLoadImage.aspx程式內容如下:
<%@ Page Inherits="UploadImage.UploadImage" SRC="UpLoadImage.cs" Language="C#"%>
<HTML><title>上傳圖片</title>
<BODY bgcolor="#FFFFFF">
<FORM ENCTYPE="multipart/form-data" RUNAT="server" ID="Form1">
<TABLE RUNAT="server" WIDTH="700" ALIGN="left" ID="Table1" cellpadding="0" cellspacing="0" border="0">
<TR>
<TD>上傳圖片(選擇你要上傳的圖片)</TD>
<TD>
<INPUT TYPE="file" ID="UP_FILE" RUNAT="server" STYLE="Width:320" ACCEPT="text/*" NAME="UP_FILE">
</TD>
</TR>
<TR>
<TD>
檔案說明(添加上傳圖片說明,如:作者、出處)
</TD>
<TD>
<asp:TextBox RUNAT="server" WIDTH="239" ID="txtDescription" MAINTAINSTATE="false" />
</TD>
</TR>
<TR>
<TD>
<asp:Label RUNAT="server" ID="txtMessage" FORECOLOR="red" MAINTAINSTATE="false" />
</TD>
<TD>
<asp:Button RUNAT="server" WIDTH="239" ONCLICK="Button_Submit" TEXT="Upload Image" />
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
//-------------------------------------------------------------------
//UpLoadImage.cs程式內容如下:
using System;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace UploadImage
{
public class UploadImage : Page {
protected HtmlInputFile UP_FILE; //HtmlControl、WebControls控制項對象
protected TextBox txtDescription;
protected Label txtMessage;
protected Int32 FileLength = 0; //記錄檔案長度變數
protected void Button_Submit(System.Object sender, System.EventArgs e) {
HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile對象,用於讀取圖象檔案屬性
FileLength = UpFile.ContentLength; //記錄檔案長度
try {
if (FileLength == 0) { //檔案長度為零時
txtMessage.Text = "<b>請你選擇你要上傳的檔案</b>";
} else {
Byte[] FileByteArray = new Byte[FileLength]; //圖象檔案臨時儲存Byte數組
Stream StreamObject = UpFile.InputStream; //建立資料流對像
//讀取圖象檔案資料,FileByteArray為資料儲存體,0為資料指標位置、FileLnegth為資料長度
StreamObject.Read(FileByteArray,0,FileLength);
//建立SQL Server連結
SqlConnection Con = new SqlConnection("Data Source=Localhost;Initial Catalog=testdb;User ID=sa;Pwd=;");
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();
txtMessage.Text = "<p><b>OK!你已經成功上傳你的圖片</b>";//提示上傳成功
}
} catch (Exception ex) {
txtMessage.Text = ex.Message.ToString();
}}}}
//----------------------------------------------------------------------
//好了,圖片已經上傳到資料庫,現在還要幹什麼呢?當然是在資料庫中讀取及顯示在Web頁中啦,請看以下程式:
//ReadImage.aspx程式內容如下:
/-----------------------------------------------------------------------
<%@ Page Inherits="ReadImage.MainDisplay" SRC="ReadImage.cs"%>
//----------------------------------------------------------------------
//ReadImage.cs程式內容如下:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ReadImage {
public class MainDisplay : System.Web.UI.Page {
public void Page_Load(System.Object sender, System.EventArgs e) {
int ImgID = Convert.ToInt32(Request.QueryString["ImgID"]); //ImgID為圖片ID
//建立資料庫連結
SqlConnection Con = new SqlConnection("Data Source=KING;Initial Catalog=testdb;User ID=sa;Pwd=;");
String SqlCmd = "SELECT * FROM ImageStore WHERE ImageID = @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, (int)SqlReader["ImageSize"]);
Response.End();
Con.Close();
//很簡單吧^_^
}
}
}
//--------------------------------------------------------------------
//最後,我們當然要把它在Web頁面顯示出來啦
//ShowImage.hml
<html>
<body>
這個是從資料庫讀取出來的圖象:<img src="ReadImage.aspx?ImgID=1">
<body>
</html>
//------------------------------------------------------------------
//最後,這程式當然還很多改進之處,希望大家多想想多編編一定可以寫出更多的圖象上傳程式
//Good Luck,engine
---------------------------------------------------------------
到www.chinabs.net去吧,那裡有原始碼下載!
---------------------------------------------------------------
Byte[] FileByteArray = new Byte[FileLength];
就是定義一個長度為FileLength,類型為Byte的數組
Dim FileByetArray(FileLength) As Byte
UP_FILE為file field的id