FileUpload上传图片到指定文件夹中,并把路径存到数据库中

解决方案 »

  1.   

    http://hi.baidu.com/kkun/blog/item/1aaba4c241e6d11b0ff47753.html
      

  2.   

     if (hPostedFile.ContentLength > 0&&hPostedFile.FileName.Length>0)
                    {                    //float zldx = hPostedFile.ContentLength / 1024;
                        filename = hPostedFile.FileName;                    int k = filename.LastIndexOf(".");
                        int j = filename.LastIndexOf("\\");
                        string type = filename.Substring(k + 1);
                        //filename = filename.Substring(j + 1, k - j - 1);
                        filename = filename.Substring(j + 1);
                        DateTime datetime1 = System.DateTime.Now;
                        type = type.ToLower();                    filepath = datetime1.ToString("yyyyMMddHHmmss") + conn.CreateRandomCode(4) + "." + type;
                        string sql3 = "insert into zjrd_dbhd_fj(hdID,title,path) values('" + GID + "','" + filename + "','" + filepath + "')";
                        cmd.CommandText = sql3;
                        cmd.ExecuteNonQuery();                    hPostedFile.SaveAs(Server.MapPath("upfile\\" + filepath));
      

  3.   

    可是我的数据库中photo的类型为Image怎么存进去啊
      

  4.   

    你不是要存路径 吗???字符串varchar就行了啊
      

  5.   


    流读取文件,写二进制字节数组代码:
       private  void InsertDB(string file)
            {
                FileInfo finfo = new FileInfo(file);   //绝对路径
                if (finfo.Exists)
                {
                    //以Northwind数据库为例子
                    SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=sa");
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "Insert into Categories(CategoryName,Description,Picture) values('test',@Description,@Content)";
                    cmd.Parameters.Add("@Content", SqlDbType.Image, (int)finfo.Length, "Picture");   //此处参数Size为写入的字节数 
                    cmd.Parameters.Add("@Description", SqlDbType.NText, (int)finfo.Length, "Description"); 
                    //读取文件内容,写入byte数组
                    byte[] content = new byte[finfo.Length];
                    FileStream stream = finfo.OpenRead();
                    stream.Read(content, 0, content.Length);
                    stream.Flush();
                    stream.Close();
               
                    cmd.Parameters["@Content"].Value = content;   //为参数赋值
                               
                     try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();                }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
      

  6.   

    上面的代码有点问题,改了一下
     private  void InsertDB(string file) 
            { 
                FileInfo finfo = new FileInfo(file);  //绝对路径 
                if (finfo.Exists) 
                { 
                    //以Northwind数据库为例子 
                    SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=sa"); 
                    SqlCommand cmd = new SqlCommand(); 
                    cmd.Connection = conn; 
                    cmd.CommandText = "Insert into Categories(CategoryName,Picture) values('test',@Content)"; 
                    cmd.Parameters.Add("@Content", SqlDbType.Image, (int)finfo.Length, "Picture");  //此处参数Size为写入的字节数 
                     //读取文件内容,写入byte数组 
                     byte[] content = new byte[finfo.Length]; 
                    FileStream stream = finfo.OpenRead(); 
                    stream.Read(content, 0, content.Length); 
                    stream.Flush(); 
                    stream.Close(); 
              
                    cmd.Parameters["@Content"].Value = content;  //为参数赋值 
                                              try 
                    { 
                        conn.Open(); 
                        cmd.ExecuteNonQuery(); 
                    } 
                    finally 
                    { 
                        conn.Close(); 
                    } 
                } 
            }