在一个ASP.Net页中实现上传照片并存入数据库,上传后显示所上传的照片
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
#region 测试写入
if (!FileUpload1.HasFile)
{
Response.Write("请选择图片");
}
string fileEx = System.IO.Path.GetExtension(this.FileUpload1.FileName); //获取图片扩展名
if (fileEx != ".gif" && fileEx != ".jpg" && fileEx != ".jpeg" && fileEx!= ".bmp")
{
Response.Write("只能上传图片");
}
else
{
// ImageType 有用,读取图片的时候需要
int imgSize = this.FileUpload1.PostedFile.ContentLength; //图片大小 (字节)
string imgType = this.FileUpload1.PostedFile.ContentType; //图片类型
string imgPath = this.FileUpload1.PostedFile.FileName; //获取图片路径
string imgName = this.FileUpload1.FileName; //获取图片名字
int imgLength = this.FileUpload1.FileName.Length; //获取图片长度
if (imgLength <= 0)//判断是否有图片
return;
Byte[] imgByte = new Byte[imgSize]; //用图片的长度来初始化一个字节数组存储临时的图片文件
Stream stream = this.FileUpload1.PostedFile.InputStream; //建立文件流对象
stream.Read(imgByte, 0, imgSize);// 读取图片数据到临时存储体imgByte,0为数据指针位置,fileLength为数据长度
//this.FileUpload1.FileBytes
try
{
string connection = ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(connection);
sqlconn.Open();
string sqlcmd = "insert into Image values (@ImageType,@ImageData,@ImageTitle)";
SqlCommand sqc = new SqlCommand(sqlcmd, sqlconn);
sqc.Parameters.Add("@ImageType", SqlDbType.VarChar, 50).Value = imgType;
sqc.Parameters.Add("@ImageData", SqlDbType.Image, imgSize).Value = this.FileUpload1.FileBytes;// imgByte;//将二进制的图片赋值给@ImageData
sqc.Parameters.Add("@ImageTitle", SqlDbType.VarChar, 50).Value = imgName;
int result = sqc.ExecuteNonQuery();
sqlconn.Close();
if (result != 0)
{
Response.Write("OK");
}
else
{
Response.Write("NO");
}
}
catch (Exception ex)
{
Response.Write(ex.StackTrace);
}
finally
{
}
}
#endregion
}
}
这个是存进去的
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
#region 测试读取
string sqlCmd = "select top 1 * from Image order by imageID desc";
string connection = ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(connection);
try
{
sqlconn.Open();
SqlCommand sqc = new SqlCommand(sqlCmd, sqlconn);
//Response.ContentType = "image/pjpeg";
using (SqlDataReader sdr = sqc.ExecuteReader())
{
while (sdr.Read())
{
//Response.ContentType = sdr["imageType"].ToString();
Response.Clear();
Response.BinaryWrite((byte[])sdr["imageData"]);
}
}
//sdr.Close();
//sqlconn.Close();
}
catch (Exception ex)
{
Response.Write(ex.StackTrace);
}
finally
{
sqlconn.Close();
}
#endregion
}
}
}
这个是取出来的,在页面要放一个容器去接收图片!