如何对浏览器上传的图片进行存储,显示在浏览器上 我是初学者请给出c#代码和详细注释谢谢了。 如何对浏览器上传的图片进行存储,显示在浏览器上 请给出c#代码和详细注释谢谢了。我要做一个网上购物系统的毕设请大家帮下忙。 ASP.ENT SQL2005 C# 我QQ174856363 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在数据库中保存图片的相对路径这是点击完上传按钮后执行的代码: //读取连接字符串 string sqlconn = System.Configuration.ConfigurationManager.AppSettings["Connectionstring"]; string filename, path; filename = Path.GetFileName(fileup.PostedFile.FileName); path = Server.MapPath("soft/"); if (Directory.Exists(path) == false) Directory.CreateDirectory(path); path += filename; if (File.Exists(path) == true) { MessageBox.Show(this.Page, "该文件已经存在!"); } else { fileup.PostedFile.SaveAs(path); string sqlstr1 = "Insert into table values()";//插入数据到表中,根据自己的表而定 SqlCommand mycomm1 = new SqlCommand(sqlstr1, myconn); if (myconn.State == ConnectionState.Close) { myconn.Open(); } if (mycomm1.ExecuteNonQuery() == 1) { MessageBox.Show(this.Page, "软件上传成功!"); } myconn.Close(); } 不知道你是要存储到数据库还是存储到指定的文件夹将图片存储到 文件夹的时候需要将图片名称存储到 数据库 还有不知道 你要怎么显示图片 绑定到什么控件//添加产品(存储过程实现) string name=this.TextBox1.Text;//名称 string cate=this.DropDownList1.SelectedItem.Value.ToString();//类别 string modle=this.TextBox2.Text;// int pric=Convert.ToInt32(this.TextBox3.Text);//价格 string pname=Path.GetFileName(this.File1.PostedFile.FileName);//获取图片路径 //判断上传文件是否为图片格式的文件 string [] jpg=pname.Split('.');//获取图片后缀 if(this.File1.PostedFile.FileName!=""&&(jpg[1]=="jpg"||jpg[1]=="jpeg"||jpg[1]=="jpe"))//判断 { this.File1.PostedFile.SaveAs(Server.MapPath("productphoto")+"\\"+pname);//保存到直盯盯饿文件夹 } string cont= this.FreeTextBox1.Text;//内容 SqlConnection conn=new SqlConnection("server=.;database=jijunwu;uid=sa;pwd=;"); SqlCommand cmd=new SqlCommand("add_product",conn); cmd.CommandType=CommandType.StoredProcedure; cmd.Parameters.Add("@p_cid",cate); cmd.Parameters.Add("@pname",name); cmd.Parameters.Add("@pcon",cont); cmd.Parameters.Add("@pmodel",modle); cmd.Parameters.Add("@upric",pric); cmd.Parameters.Add("@pphoto",pname); conn.Open(); int i=cmd.ExecuteNonQuery(); //判断上传文件是否为图片格式的文件,若是上传成功否则失败 if(i==1&&(jpg[1]=="jpg"||jpg[1]=="jpeg"||jpg[1]=="jpe")) { Response.Write("<Script>alert('产品添加成功!')</Script>"); } else { Response.Write("<Script>alert('产品添加失败!')</Script>"); } conn.Close(); 绑定到DataListSqlConnection conn=new SqlConnection("server=.;database=jijunwu;uid=sa;pwd=;");//创建数据库连接对象 SqlDataAdapter sda=new SqlDataAdapter("bindproduct",conn); sda.SelectCommand.CommandType=CommandType.StoredProcedure;//声明执行的是存储过程 DataSet ds=new DataSet(); sda.Fill(ds,"products");//执行 this.DataList1.DataSource=ds;//指定数据源 this.DataList1.DataBind();//绑定数据源 <asp:DataList id="DataList1" runat="server" Width="485px" RepeatDirection="Horizontal" RepeatColumns="3"><ItemTemplate> <table><tr><td colspan="2"> <img src='<%#DataBinder.Eval%></td></tr><!--显示图片--><tr><td>产品名称:</td><td><%#DataBinder.Eval(Container.DataItem,"pro_name")%></td></tr><tr><td>产品型号:</td><td><%#DataBinder.Eval(Container.DataItem,"pro_model")%></td></tr><tr><td>产品价格:</td><td><%#DataBinder.Eval(Container.DataItem,"unitprice")%></td></tr></table></ItemTemplate></asp:DataList> http://blog.csdn.net/21aspnet/archive/2004/10/27/155164.aspx http://blog.csdn.net/21aspnet/category/49070.aspx?PageNumber=1 参考下面四篇:http://blog.csdn.net/insus/archive/2008/01/20/2055033.aspxhttp://blog.csdn.net/insus/archive/2008/01/20/2055030.aspxhttp://www.cnblogs.com/insus/articles/1430434.htmlhttp://www.cnblogs.com/insus/articles/1425489.html http://hi.baidu.com/lyn00750/blog/item/050ec803bf3a1e763812bb3c.html http://download.csdn.net/source/426218图片上传并显示(ASP.NET) asp.net里文件最大上传限制是多少 新手问个DropDownList的简单问题 动态创建的GridView分页的问题 自己总结的c# 数据库连接跟操作 多多批评,我不大会 我大三,下半年想找工作,应该注意什么啊 怎样在前面的窗口打开新页面 请教 WebService 对外 发布接口,应注意的? Cookies购物车问题,欢迎解答 一个分页的问题 专家请进:能否在用VS。NET作为开发平台开发ASP。NET应用系统的时候编译成相应的CS文件的DLL文件而不是一个DLL文件 ASP.NET(c#)中关闭页面之前所触发的事件是什么?。。 怎样将dataset内容缓存在客户端
这是点击完上传按钮后执行的代码: //读取连接字符串
string sqlconn = System.Configuration.ConfigurationManager.AppSettings["Connectionstring"];
string filename, path;
filename = Path.GetFileName(fileup.PostedFile.FileName);
path = Server.MapPath("soft/");
if (Directory.Exists(path) == false)
Directory.CreateDirectory(path);
path += filename;
if (File.Exists(path) == true)
{
MessageBox.Show(this.Page, "该文件已经存在!");
}
else
{
fileup.PostedFile.SaveAs(path);
string sqlstr1 = "Insert into table values()";//插入数据到表中,根据自己的表而定
SqlCommand mycomm1 = new SqlCommand(sqlstr1, myconn);
if (myconn.State == ConnectionState.Close)
{
myconn.Open();
}
if (mycomm1.ExecuteNonQuery() == 1)
{
MessageBox.Show(this.Page, "软件上传成功!");
}
myconn.Close();
}
将图片存储到 文件夹的时候需要将图片名称存储到 数据库
还有不知道 你要怎么显示图片 绑定到什么控件//添加产品(存储过程实现)
string name=this.TextBox1.Text;//名称
string cate=this.DropDownList1.SelectedItem.Value.ToString();//类别
string modle=this.TextBox2.Text;//
int pric=Convert.ToInt32(this.TextBox3.Text);//价格 string pname=Path.GetFileName(this.File1.PostedFile.FileName);//获取图片路径
//判断上传文件是否为图片格式的文件
string [] jpg=pname.Split('.');//获取图片后缀
if(this.File1.PostedFile.FileName!=""&&(jpg[1]=="jpg"||jpg[1]=="jpeg"||jpg[1]=="jpe"))//判断
{
this.File1.PostedFile.SaveAs(Server.MapPath("productphoto")+"\\"+pname);//保存到直盯盯饿文件夹
}
string cont= this.FreeTextBox1.Text;//内容 SqlConnection conn=new SqlConnection("server=.;database=jijunwu;uid=sa;pwd=;");
SqlCommand cmd=new SqlCommand("add_product",conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add("@p_cid",cate);
cmd.Parameters.Add("@pname",name);
cmd.Parameters.Add("@pcon",cont);
cmd.Parameters.Add("@pmodel",modle);
cmd.Parameters.Add("@upric",pric);
cmd.Parameters.Add("@pphoto",pname);
conn.Open();
int i=cmd.ExecuteNonQuery();
//判断上传文件是否为图片格式的文件,若是上传成功否则失败
if(i==1&&(jpg[1]=="jpg"||jpg[1]=="jpeg"||jpg[1]=="jpe"))
{
Response.Write("<Script>alert('产品添加成功!')</Script>");
}
else
{
Response.Write("<Script>alert('产品添加失败!')</Script>");
}
conn.Close();
SqlConnection conn=new SqlConnection("server=.;database=jijunwu;uid=sa;pwd=;");//创建数据库连接对象
SqlDataAdapter sda=new SqlDataAdapter("bindproduct",conn);
sda.SelectCommand.CommandType=CommandType.StoredProcedure;//声明执行的是存储过程
DataSet ds=new DataSet();
sda.Fill(ds,"products");//执行
this.DataList1.DataSource=ds;//指定数据源
this.DataList1.DataBind();//绑定数据源 <asp:DataList id="DataList1" runat="server" Width="485px" RepeatDirection="Horizontal" RepeatColumns="3">
<ItemTemplate>
<table>
<tr><td colspan="2"> <img src='<%#DataBinder.Eval%></td></tr><!--显示图片-->
<tr><td>产品名称:</td><td><%#DataBinder.Eval(Container.DataItem,"pro_name")%></td></tr>
<tr><td>产品型号:</td><td><%#DataBinder.Eval(Container.DataItem,"pro_model")%></td></tr>
<tr><td>产品价格:</td><td><%#DataBinder.Eval(Container.DataItem,"unitprice")%></td></tr>
</table>
</ItemTemplate>
</asp:DataList>
http://blog.csdn.net/insus/archive/2008/01/20/2055033.aspx
http://blog.csdn.net/insus/archive/2008/01/20/2055030.aspx
http://www.cnblogs.com/insus/articles/1430434.html
http://www.cnblogs.com/insus/articles/1425489.html
图片上传并显示(ASP.NET)