//
开发环
境:Window 2000、SQLServer2000、.Net Framework SDK正式版 //
开发语
言:C#、ASP.Net //
简
介:数据
库
中
图
片存蓄及
读
取 //作者:engine /*
说
明:在ASP中,我
们
用Request.TotalBytes、Request.BinaryRead()来上
传图
片,
这
个可
恶
的BinaryRead()方法非常笨,
单
个文件上
传
倒没什
么
大事,
单
如果多个
图
片上
专
可就花大气力了…!而
现
在ASP.Net中将会把解决以前ASP中文件上
传
的
种种问题
,使你在ASP.Net中
轻轻
松松
开发
出功能
强
大的上
传
程序,下面大家看看例子啦。 */ //注意:由于作者水平有限,
错误
是
难
免的,如
发现错误请
指教 //Email:e_engine@21cn.com
/*
首先在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