上传图片到数据库的问题???高分请教! trybyte[] btImage = ( byte[] )reader[ photo ];Response.BinaryWrite( btImage );Response.End(); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 怎样上传图片,把图片保存到sql server数据库里,有源代码最好。 --------------------------------------------------------------- 先在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 seeFile Upload with ASP.NEThttp://www.codeproject.com/aspnet/FileUpload.asp 你可以参考一下下面的文章:http://www.csdn.net/develop/read_article.asp?id=17711 http://www.chinabs.net/download/default.asp上传图片到数据库 ASP.NET 该例实现了上传图片到数据库,并读出显示在网页上. 4471 点击下载 我也试过,很是好用 string.Empty在什么情况下使用! moo.jx 折叠问题,望高手帮忙 如何作这样查询 异常详细信息: System.Data.OleDb.OleDbException: 未指定的错误, 关于eWebEditor asp.net 2.0当中还有WebUIValidation.js吗? asp.net怎样不通过远程服务器发信,而通过自身来发信。 如何找到匹配括号{}的另一半 求一常用的正则表达式 Page_load不能声明为overrides,因为它不重写class中的sub DataGrid中的模板问题 在NT4.0 workstation下可以用哪个IIS版本?哪有下载啊?
---------------------------------------------------------------
先在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
File Upload with ASP.NET
http://www.codeproject.com/aspnet/FileUpload.asp
http://www.csdn.net/develop/read_article.asp?id=17711
我也试过,很是好用