我参考别人的做法,自己写了一个,可是说“未将对象实例化”,我看了半天也没看出来问题在哪。请大哥们指教。
 protected void Button1_Click(object sender, EventArgs e)
    {
        int   intImageSize;   
        string   strImageType;   
        Stream   ImageStream;
       
        intImageSize = personImage.PostedFile.ContentLength ;  //   文件大小   
        strImageType = personImage.PostedFile.ContentType;   //   文件类型   
        ImageStream   =   personImage.PostedFile.InputStream;   
        byte[]   ImageContent   =   new   byte[intImageSize];   
        int   intStatus   =   ImageStream.Read(ImageContent,   0,   intImageSize);   
   
         //   写入数据库   
        string   strConn   =   "Data Source=(local);initial catalog=xiaoyuan;uid=sa;pwd=sa";   
        SqlConnection   myConnection   =   new   SqlConnection(strConn);   
        SqlCommand   myCommand   =   new   SqlCommand("z_UserInfo",   myConnection);   
        myCommand.CommandType   =   CommandType.StoredProcedure;   
       
         
         
        myCommand.Parameters.Add("@User_image",   SqlDbType.Image).Value   =   ImageContent;   
        myCommand.Parameters.Add("@User_imageType",   SqlDbType.VarChar,   255).Value   =   strImageType;   
   
        try   
        {   
            myConnection.Open();   
            myCommand.ExecuteNonQuery();   
            myConnection.Close();   
            Response.Write("添加成功!");   
        }   
        catch(System.Exception   SQLExe)   
        {   
            Response.Write("添加失败!原因:"+SQLExe.ToString());   
        }   
    }
出错的是intImageSize = personImage.PostedFile.ContentLength ; ,说“未将对象实例化”“,求大哥们帮看看,小弟不盛感激

解决方案 »

  1.   

    图片放数据库里不好..不如放个路径更好personImage在哪里定义?
      

  2.   

    如果让客户上传图片,是不是应该将图片放在数据库里?personImage是HtmlInputFile控件,我在页面里拖控件时命名的,然后在public partial class Insert_Image : System.Web.UI.Page里声明了,protected System.Web.UI.HtmlControls.HtmlInputFile personImage;,我虽然用new定义了,但是还是出现上述错误。
      

  3.   

    http://www.dotnetxx.cn/shownews.aspx?id=58
      

  4.   

    这是添加 string sql = "insert into TestTable (TGuid,TName,TSex,TMemo,TWork,Timage,updatatime)values(newid(),@TName,@TSex,@TMemo,@TWork,@Timage,getdate())";
                HGRD.DBHelper.SqlDataParam sp = new HGRD.DBHelper.SqlDataParam();
                sp.Add("@TName", this.TextBox1.Text);
                sp.Add("@TSex", this.TextBox2.Text == "男" ? "1" : "0");
                sp.Add("@TWork", this.TextBox3.Text);
                sp.Add("@TMemo", this.TextBox4.Text);
                int len = FileUpload1.PostedFile.ContentLength;
                byte[] pic = new byte[len];
                this.FileUpload1.PostedFile.InputStream.Read(pic, 0, len);
                sp.Add("@TIMAGE", pic);
                int i = HGRD.DBHelper.WebDBHelper.ExecuteNonQuery(sql, sp);
                if (i == 0)
                    Response.Write("添加失败!");
                else
                    Response.Write("添加成功!");
      

  5.   

    这是读取public class Handler : IHttpHandler {
        
        public void ProcessRequest (HttpContext context) {
            context.Response.ContentType = "image/jpeg";        context.Response.Cache.SetCacheability(HttpCacheability.Public);        context.Response.BufferOutput = false;        if (context.Request.QueryString["guid"] == null) return;        Stream stream = GetPhoto(context.Request.QueryString.Get("guid"));//==★Get the photo stream        const int buffersize = 1024 * 16;        byte[] buffer = new byte[buffersize];        int count = stream.Read(buffer, 0, buffersize);//==★the from position is always 0?        while (count > 0)
            {            context.Response.OutputStream.Write(buffer, 0, count);            count = stream.Read(buffer, 0, buffersize);        }    }
     
        public bool IsReusable {
            get {
                return false;
            }
        }
        public Stream GetPhoto(string guid)
        {         string sql = "select TImage  from TestTable where TGUID='{0}'";
             sql = string.Format(sql, guid);
             object result = HGRD.DBHelper.WebDBHelper.ExecuteScalar(sql);//==★return the first Column of the first Row
            try
            { //==return new MemoryStream(CType(result,byte()))            return new MemoryStream((byte[])result);//==★if use vb,how to replace this word.        }        catch (ArgumentNullException e)
            {            return null;        }        
        }}
      

  6.   

    http://dugupiaoyun.icode.csdn.net/post/2007/05/26/53730
      

  7.   

    to:lionelwy(顺) ,几乎完全照搬您的代码,可是还是说“未将对象实例化”,错在
    HttpPostedFile UpFile = Up_file.PostedFile;这行代码using System;
    using System.Data;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.IO;
    using System.Collections;
    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;public partial class UploadImage : System.Web.UI.Page
    {
        protected HtmlInputFile Up_file;
            protected Int32 fileLength = 0;
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpPostedFile UpFile = Up_file.PostedFile;
            fileLength = UpFile.ContentLength;
            try
            {
                if (fileLength == 0)
                {
                    LB_message.Text = "<b>请你选择你要上传的文件</b>";
                }
                else
                {
                    Byte[] FileByteArray = new Byte[fileLength];
                    Stream StreamObject = UpFile.InputStream;
                    StreamObject.Read(FileByteArray, 0, fileLength);
                    SqlConnection Con = new SqlConnection("Data Source=(local);Initial Catalog=xiaoyuan;uid=sa;pwd=sa;");
                    string SqlCmd = "insert into z_UserInfo(User_image,User_imageType)values(@User_image,@User_imageType)";
                    SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
                    CmdObj.Parameters.Add("@User_image", SqlDbType.Binary, fileLength).Value = FileByteArray;
                    CmdObj.Parameters.Add("@User_imageType", SqlDbType.VarChar, 50).Value = UpFile.ContentType;
                   
                    Con.Open();
                    CmdObj.ExecuteNonQuery();
                    Con.Close();
                    LB_message.Text = "<p><b>OK!你已经成功上传你的图片</b>";            }
            }
            catch (Exception ex)
            {
               LB_message.Text = ex.Message.ToString();
            }    }
    }
      

  8.   

    to:dugupiaoyun(独孤飘云) 
    我想做的是B/S结构的,您的代码是C/S结构的,不过仍然非常感谢
      

  9.   

    回复:
    可是还是说“未将对象实例化”,错在 
    HttpPostedFile   UpFile   =   Up_file.PostedFile;这行代码 正解:
    <INPUT id="Up_file" type="file" size="52" runat="server">记得要写上:runat="server"