公司不能上网,代码不能拿到,
其实就是那几行了  将img写入MemoryStream 就出错,我都快风掉了

解决方案 »

  1.   

    private void Button1_ServerClick(object sender, System.EventArgs e)
    {
    Stream imgStream;
    int imgLen;
    string imgName_value;
    string imgContentType;
    string imgUploadedName;
       
    imgStream = UploadFile.PostedFile.InputStream;
    imgLen =  UploadFile.PostedFile.ContentLength;
    imgUploadedName = UploadFile.PostedFile.FileName;
    byte[] imgBinaryData=new byte[imgLen];
    imgContentType = UploadFile.PostedFile.ContentType;

    try
    {
    imgName_value = GetLastRightOf(".",imgUploadedName); if (imgName_value == "nopic")
    {
    Response.Write( "<script>window.alert('所选文件不是图形文件,请选择图形文件上传!');</script>" );
    return;
    }
    }
    catch(Exception myEx)
    {
    Response.Write(myEx.Message);
    }

    int n = imgStream.Read(imgBinaryData, 0, imgLen);  
    string sheetid = Request.QueryString ["sheetid"].ToString ();
    string patternname = Request.QueryString ["patternname"].ToString ();
                string imgid = DataBase.DataFunction.SearchField ("ID","ws_pattern","patternname='"+patternname+"'"); int NumRowsAffected = MyDatabaseMethod(sheetid,patternname,imgid,imgBinaryData);
    if(NumRowsAffected > 0)
    {
    Response.Write( "<script>window.close();</script>" );
    }
    else
    Response.Write( "<BR> an error occurred uploading the image.d " );
    }

    public string GetLastRightOf(string LookFor,string myString)
    {
    int StrPos;
    StrPos = myString.LastIndexOf(LookFor);
    string temp = myString.Substring(StrPos + 1);

    //判断是否图片文件
    if (temp != "bmp" ) //|| temp != "gif")
    {
    return "nopic";
    }
    else
    {
    return myString.Substring(StrPos + 1);
    }
    }
      

  2.   

    public int MyDatabaseMethod(string sheetid,string patternname,string imgid,byte[] imgbin)
    {
    string strconn = ConfigurationSettings.AppSettings["strConnection"];
    SqlConnection connection = new SqlConnection(strconn);

    //SqlConnection connection = new SqlConnection(Application["Test_Conn"].ToString());
    string isfind = DataBase.DataFunction.SearchField ("SHEETID","WS_SHEETIMAGE"," SHEETID="+Session["sheetid"].ToString());
    string SQL="";
    if (isfind != "")
    {
    //已有图片,改为修改记录
    SQL="update ws_sheetimage set IMAGE = @IMAGE where SHEETID = " +Session["sheetid"].ToString();
    }
    else
    {
    SQL="INSERT INTO ws_sheetimage (SHEETID,PATTERNNAME,ID,IMAGE) VALUES ( @SHEETID, @PATTERNNAME,@ID,@IMAGE )";
    }
    SqlCommand command=new SqlCommand ( SQL,connection );
                
    SqlParameter param0=new SqlParameter ( "@SHEETID", SqlDbType.VarChar,50 );
    param0.Value = sheetid;   
    command.Parameters.Add( param0 );            

    SqlParameter param1=new SqlParameter ( "@PATTERNNAME", SqlDbType.VarChar,30 );   
    param1.Value = patternname;
    command.Parameters.Add( param1 );
                
    SqlParameter param2 =new SqlParameter ( "@ID", SqlDbType.SmallInt,2 );
    param2.Value = imgid;
    command.Parameters.Add( param2 ); SqlParameter param3=new SqlParameter ( "@IMAGE", SqlDbType.Image );   
    param3.Value = imgbin;
    command.Parameters.Add( param3 );
       
    connection.Open();
    int numRowsAffected = command.ExecuteNonQuery();
    connection.Close();
    return numRowsAffected;
    }
      

  3.   

    没有那么麻烦了,你使用BinaryFormatter.Serialize就可以将对象保存为byte[]的了
      

  4.   

    FileStream fs = new FileStream ("e:\\1.bmp", FileMode.OpenOrCreate, 
    FileAccess.Read);
    BinaryReader reader = new BinaryReader(fs);int i = Convert.ToInt32(fs.Length);
    byte[] MyData = new Byte[i];
    MyData = (byte[])reader.ReadBytes(i);//在这里跟踪下MyData看看
      

  5.   

    在sql server中存取图片看下边的:
    http://www.c-sharpcorner.com/Code/2002/Feb/FlashCardsMG.asp