首先在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>