如何对浏览器上传的图片进行存储,显示在浏览器上 请给出c#代码和详细注释谢谢了。我要做一个网上购物系统的毕设请大家帮下忙。 
ASP.ENT SQL2005 C#  我QQ174856363 

解决方案 »

  1.   

    在数据库中保存图片的相对路径
    这是点击完上传按钮后执行的代码:            //读取连接字符串
                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();
                }
      

  2.   

    不知道你是要存储到数据库还是存储到指定的文件夹
    将图片存储到 文件夹的时候需要将图片名称存储到 数据库  
    还有不知道 你要怎么显示图片 绑定到什么控件//添加产品(存储过程实现)
    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();
      

  3.   

    绑定到DataList
    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>
      

  4.   

    http://blog.csdn.net/21aspnet/archive/2004/10/27/155164.aspx
      

  5.   

    http://blog.csdn.net/21aspnet/category/49070.aspx?PageNumber=1
      

  6.   

    参考下面四篇:
    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
      

  7.   

    http://hi.baidu.com/lyn00750/blog/item/050ec803bf3a1e763812bb3c.html
      

  8.   

    http://download.csdn.net/source/426218
    图片上传并显示(ASP.NET)