在VS2005开发平台上 怎么利用HTML 中的input(file)控件传照片到数据库啊?别的方法也中~

解决方案 »

  1.   

     private string UploadPicFile(System.Web.UI.HtmlControls.HtmlInputFile Fupload)
        {
            //文件上传
            try
            {
                if (Fupload.PostedFile.FileName == "")
                    return "";
                string dir = DateTime.Now.ToString("yyyy");
                if (!Directory.Exists(Server.MapPath("uploadpic")))
                {
                    Directory.CreateDirectory(Server.MapPath("uploadpic"));
                    if (!Directory.Exists(Server.MapPath("uploadpic")))
                        return "";
                }
                Random rd = new System.Random();
                string filename;
                string extname;            if (Fupload.PostedFile.FileName != "")
                {
                    extname = Fupload.PostedFile.FileName.Substring(Fupload.PostedFile.FileName.LastIndexOf(".") + 1).ToUpper();
                    if ("JPG".IndexOf(extname) == -1)
                    {
                        return "";
                    }                filename = dir + "\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + rd.Next(1000).ToString() + "." + extname;
                    string mfileExtension = System.IO.Path.GetExtension(filename);
                    Fupload.PostedFile.SaveAs(Server.MapPath("uploadpic\\") + filename);
                    return "uploadpic\\" + filename;
                }
                return "";        }
            catch { return ""; }
        }
      

  2.   


                              <input id="File2" type="file" runat="server" /> string imgUrl = this.UploadPicFile(this.File1);
            if (imgUrl == "")
            {
                            return;
            }
      

  3.   

    给你来段代码public static string path;
     protected void btnGoUp_Click(object sender, EventArgs e)
        {
            string str = this.File1.PostedFile.FileName;
            if (str == "")
            {
                Response.Write(bc.MassageBox("请选择上传图片!"));
                return;
            }        string ss = str.Substring(str.LastIndexOf("\\") + 1);
            string s = Server.MapPath("~\\photo\\" + ss);
            path = "~\\photo\\" + ss;
            if (File.Exists(s))
            {
                Response.Write(bc.MassageBox("该文件已经存在,请重新命名!!!"));
                return;
            }
            this.File1.PostedFile.SaveAs(s);
            Response.Write(bc.MassageBox("上传成功!"));
        }
    存数据库的时候就存path就是当前图片的路径了页面这样<input id="File1" runat="server" class="InputCss" style="width: 287px" type="file" />
                            <asp:Button ID="btnGoUp" runat="server" CausesValidation="False" CssClass="redButtonCss"
                                OnClick="btnGoUp_Click" Text="上传" Width="67px" style="position: relative" />
      

  4.   

    先谢谢啦,可为什么我在输入的时候说找不到“file1”?
      

  5.   

    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)
        {
            //type == "jpg" || type == "png" || type == "bmp" || type == "gif" || type == "jpeg"
        }    string str = "insert into FileImage(content) values(@content)";
        string Conn = @"server=.;uid=sa;pwd=sa;database=Northwind";    protected void btnFileUpload_Click(object sender, EventArgs e)
        {
          //string imgtype = .ContentType;                    //文件类型 
             string sFile = FileUpload1.PostedFile.FileName.ToLower();
            string type = Path.GetExtension(sFile);
            string FileType = type.Substring(type.IndexOf('.') + 1);      //FileName.Substring(FileName.IndexOf('.') + 1);        if (FileType == "jpg")
            {            byte[] content = FileToByte(sFile);
                SaveFile(content);
                Response.Write("<script language='javascript'>alert('上传成功');</script>");      
            }
            else
            {        
                Response.Write("<script language='javascript'>alert('图片上传失败,请重试!');</script>");
                       
            }
          
           
        }
        private byte[] FileToByte(string filepath)
        {
            byte[] ib = new byte[6000];
           
            //byte[] ib = new byte[6000];
            //获取上传的图片路径
            string strFilepath = filepath;
           FileStream fs = new FileStream(strFilepath, FileMode.Open, FileAccess.Read);
           //将文件的内容读进字节数组
           fs.Read(ib, 0, filepath.Length);
           return ib;
       //  Response.Write("<script language='javascript'>alert('图片上传失败,请重试!" + ex.Message.ToString() + "');</script>");      
          
        }
       
        private void SaveFile(byte[] content)
        {
                  SqlConnection my_conn = new SqlConnection(Conn);
            my_conn.Open();        SqlCommand sql_cmd = new SqlCommand(str, my_conn);        sql_cmd.Parameters.Add("@content",SqlDbType.Image).Value = content;        sql_cmd.ExecuteNonQuery();
           //sql_cmd.ExecuteReader();
           
        }    }
    }
      

  6.   

    添加runat="server"
    参考
    http://www.cnblogs.com/HurYun/articles/384286.html
    http://www.cnblogs.com/kingjiong/archive/2008/01/13/1037084.html