ASP.NET中存取图片到数据库的示例

 
// 开发环 境: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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值