<%@ WebHandler Language="C#" Class="ImageHandler" %>using System; using System.Web; using System.Data.SqlClient;public class ImageHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) { string imageid = context.Request.QueryString["ImID"]; SqlConnection connection = new SqlConnection("Data Source=(local);Database=wxd;Uid=sa;Pwd=sa"); connection.Open(); SqlCommand command = new SqlCommand("select [Image] from [Image] where ImageID=" + imageid, connection); SqlDataReader dr = command.ExecuteReader(); dr.Read(); context.Response.BinaryWrite((Byte[])dr[0]); connection.Close(); context.Response.End(); }
去看看
public void bind()
{
string sqlstr = "select [ImageName],[ImageID] from [Image]";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "Admin");
GridView1.DataSource = myds;
GridView1.DataBind();
}
protected void Bt_Cacel_Click(object sender, EventArgs e)
{
this.Panel2.Visible = false;
}
//保存图片
protected void Bt_Save_Click(object sender, EventArgs e)
{
Stream imgStream=FU_image.PostedFile.InputStream;
int imgLen=FU_image.PostedFile.ContentLength;
string imgName=this.T_image.Text;
byte[] imgBinaryData=new byte[imgLen];
int n=imgStream.Read(imgBinaryData,0,imgLen);
SqlConnection connection = new SqlConnection("Data Source=(local);Database=wxd;Uid=sa;Pwd=sa");
SqlCommand command = new SqlCommand("insert into Image (ImageName,Image) values ( @img_name, @img_data)", connection);
SqlParameter param0 = new SqlParameter("@img_name", SqlDbType.VarChar, 50);
param0.Value = imgName;
command.Parameters.Add(param0);
SqlParameter param1 = new SqlParameter("@img_data", SqlDbType.Image);
param1.Value = imgBinaryData;
command.Parameters.Add(param1);
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();
this.Panel2.Visible = false;
this.T_image.Text = "";
bind();
}
//显示保存
{
if (FileUpload1.HasFile)
{
string fileContentType = FileUpload1.PostedFile.ContentType;//获取客户端用户发送的文件类型
if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")//判断图片的类型
{
string name = FileUpload1.PostedFile.FileName; // 客户端文件路径 FileInfo file = new FileInfo(name);
string fileName = file.Name; // 文件名称
string fileName_s = "x_" + file.Name; // 缩略图文件名称
string fileName_sy = "text_" + file.Name; // 水印图文件名称(文字)
string fileName_syp = "water_" + file.Name; // 水印图文件名称(图片)
string webFilePath = Server.MapPath(fileName); // 服务器端文件路径
string webFilePath_s = Server.MapPath(fileName_s); // 服务器端缩略图路径
string webFilePath_sy = Server.MapPath(fileName_sy); // 服务器端带水印图路径(文字)
string webFilePath_syp = Server.MapPath(fileName_syp); // 服务器端带水印图路径(图片) if (!File.Exists(webFilePath))//判断服务器端文件是否存在
{
try
{
//将信息保存在数据库中
byte[] fileData = FileUpload1.FileBytes;
SqlConnection con = new SqlConnection(@"server=.;uid=sa;pwd=sa;database=Wish");
SqlCommand com = new SqlCommand("InsertChanPin", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add("@cpName", SqlDbType.VarChar, 50).Value = txtName.Text;
com.Parameters.Add("@cpId", SqlDbType.VarChar,50).Value = txtId.Text;
com.Parameters.Add("@cpImages", SqlDbType.Image).Value =fileData;
con.Open();
com.ExecuteNonQuery();
con.Close();
FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B";
}
catch (Exception ex)
{
Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message;
}
}
else
{
Label1.Text = "提示:文件已经存在,请重命名后上传";
}
}
else
{
Label1.Text = "提示:文件类型不符";
}
}
}这个是我的后台代码!!!!!!
<asp:BoundField DataField="ImageID" HeaderText="编号" />
<asp:BoundField DataField="ImageName" HeaderText="用户名称" />
<asp:TemplateField HeaderText="头像">
<ItemTemplate>
<asp:Image ID="Image1" Height="80" Width="100" runat="server" ImageUrl='<%# "ImageHandler.ashx?ImID="+ Eval("ImageID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
using System.Web;
using System.Data.SqlClient;public class ImageHandler : IHttpHandler {
public void ProcessRequest (HttpContext context)
{
string imageid = context.Request.QueryString["ImID"];
SqlConnection connection = new SqlConnection("Data Source=(local);Database=wxd;Uid=sa;Pwd=sa");
connection.Open();
SqlCommand command = new SqlCommand("select [Image] from [Image] where ImageID=" + imageid, connection);
SqlDataReader dr = command.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((Byte[])dr[0]);
connection.Close();
context.Response.End(); }
public bool IsReusable
{
get {
return false;
}
}}
我主要做的一个网站是从后台添加产品,然后在产品的页面里面将后台传过去的读出来,能按我的表的设计详细点吗?我是新手,谢谢!!!!