1)在Oracle数据表中有一个blob型的pic字段,我想在Web窗体上通过单击一个浏览按钮来打开本地上的一个图片并显示在Image1控件上,然后通过单击提交按钮来将图片存储到表中的pic字段里,怎么做?
2)如果我要在图片浏览页上浏览已有的图片(如:我点击某个图片的连接)怎么将该图片从数据库中读取并显示在该页上的Image1控件上?

解决方案 »

  1.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=2A5DD7C6-A45A-48AB-A2E8-342A29F17506
      

  2.   

    可以用流开试吧,这这我不懂.
    我的做法是,数据库字段为字符型,在数据库中存一个路径,在服务器上图片别存为,本地的图片名,与存在数据库中的名称一样.
    取图片那就简单多了.public string UpImg()
    {
    string filetype;
    filetype=File1.PostedFile.ContentType;
    if(File1.PostedFile.FileName=="")
    {
    return "noimg";
    } if(filetype!="image/gif" && filetype!="image/pjpg " && filetype!="image/pjpeg" && filetype!="image/bmp")
    {
    Response.Write("<script>alert('对不起!你上传的应该是图片!')</script>");
    return "error";
    }
    else
    {
    System.Drawing.Image img= System.Drawing.Image.FromStream(File1.PostedFile.InputStream);
    int Width = img.Width;
    int Height = img.Height;
    if(Width>200 || Height>100 || File1.PostedFile.ContentLength>1024*1024*2)
    {
    Response.Write("<script>alert('对不起!文件尺寸或大小不对!')</script>");
    return "error";
    }
    else
    {
    int L;
    L=File1.PostedFile.FileName.Length;
    L=L-4;
    filetype=File1.PostedFile.FileName.Substring(L,4);
    string sPath=Server.MapPath("")+"\\"+"img"+"\\";
    sPath+=Session["userid"].ToString()+house.GetDateTime().Replace(" ",null)+filetype;
    this.File1.PostedFile.SaveAs(sPath);
    Response.Write("<script>alert('图片上传成功!')</script>");
    return "\\"+"img"+"\\"+Session["userid"].ToString()+house.GetDateTime().Replace(" ",null)+filetype;
    } }多个文件上传
    namespace HowTos.MultipleImageUpdate
    {
    public class UPLOAD : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.LinkButton LinkButton1; #region User Defined Code

    protected System.Web.UI.WebControls.Label Label1;

    private void Page_Load(System.Object sender, System.EventArgs e)
    {
    if ( this.IsPostBack ) 
    this.SaveImages();
    } private System.Boolean SaveImages() {
    //loop through the files uploaded System.Web.HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;

    //Message to the user
    System.Text.StringBuilder _message = new System.Text.StringBuilder("Files Uploaded:<br>");

    try 
    {
    for ( System.Int32 _iFile = 0; _iFile < _files.Count; _iFile ++ ) 
    {

    // Check to make sure the uploaded file is a jpg or gif

    System.Web.HttpPostedFile _postedFile = _files[_iFile]; 
    System.String _fileName, _fileExtension; _fileName = System.IO.Path.GetFileName(
    _postedFile.FileName);

    _fileExtension = System.IO.Path.GetExtension(
    _fileName); if ( _fileExtension == ".gif" ) 
    {

    //Save File to the proper directory
    _postedFile.SaveAs( 
    System.Web.HttpContext.Current.Request.MapPath(
    "gifs/") + _fileName);
    _message.Append(_fileName + "<BR>");

    }
    else if ( _fileExtension == ".jpg" ) 
    { //Save File to the proper directory
    _postedFile.SaveAs( 
    System.Web.HttpContext.Current.Request.MapPath(
    "jpgs/") + _fileName);
    _message.Append(_fileName + "<BR>");


    else {

    _message.Append(_fileName + " <font color=\"red\">failed!! Only .gif and .jpg images allowed!</font> <BR>");

    } }

    Label1.Text = _message.ToString();
    return true;
    }
    catch ( System.Exception Ex ) 


    Label1.Text = Ex.Message ;
    return false;   

    } }
    这是图片上传的代码.
      

  3.   

    用流,存入数据库用 SqlParameter
      

  4.   

    http://www.chinamacro.com/blog/visit_detail.aspx?blogID=53
      

  5.   

    namespace UploadSample {
    public class Main : System.Web.UI.Page {
    protected System.Web.UI.HtmlControls.HtmlInputFile UP_FILE;protected System.Web.UI.WebControls.TextBox txtDescription;protected System.Web.UI.WebControls.Label txtMessage;protected System.Int32 FileLength = 0;
    protected void Button_Submit(System.Object sender, System.EventArgs e) {System.Web.HttpPostedFile UpFile = UP_FILE.PostedFile;FileLength = UpFile.ContentLength;
    try {
    if (FileLength == 0) {
    txtMessage.Text = "<b>*请选择上传的文件</b>";
    } else {
    System.Byte[] FileByteArray = new System.Byte[FileLength];System.IO.Stream StreamObject = UpFile.InputStream;StreamObject.Read(FileByteArray,0,FileLength);System.Data.OleDb.OleDbConnection Con = new System.Data.OleDb.OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;" +"Integrated Security=SSPI;Initial Catalog=northwind");System.String SqlCmd = "INSERT INTO Images (Image, ContentType, ImageDescription, ByteSize) VALUES (?, ?, ?, ?)";System.Data.OleDb.OleDbCommand OleDbCmdObj = new System.Data.OleDb.OleDbCommand(SqlCmd, Con);OleDbCmdObj.Parameters.Add("@Image", System.Data.OleDb.OleDbType.Binary, FileLength).Value = FileByteArray;OleDbCmdObj.Parameters.Add("@ContentType", System.Data.OleDb.OleDbType.VarChar,50).Value = UpFile.ContentType;OleDbCmdObj.Parameters.Add("@ImageDescription", System.Data.OleDb.OleDbType.VarChar,100).Value = txtDescription.Text;OleDbCmdObj.Parameters.Add("@ByteSize", System.Data.OleDb.OleDbType.VarChar,100).Value = UpFile.ContentLength;Con.Open();OleDbCmdObj.ExecuteNonQuery();Con.Close();txtMessage.Text = "<p><b>* 图片上传成功!</b>";
    }
    } catch (System.Exception ex) {
    txtMessage.Text = ex.Message.ToString();
    }}}}
      

  6.   

    System.Data.OleDb.OleDbConnection Con = new System.Data.OleDb.OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;" +"Integrated Security=SSPI;Initial Catalog=northwind");System.String SqlCmd = "INSERT INTO Images (Image, ContentType, ImageDescription, ByteSize) VALUES (?, ?, ?, ?)";System.Data.OleDb.OleDbCommand OleDbCmdObj = new System.Data.OleDb.OleDbCommand(SqlCmd, Con);OleDbCmdObj.Parameters.Add("@Image", System.Data.OleDb.OleDbType.Binary, FileLength).Value = FileByteArray;OleDbCmdObj.Parameters.Add("@ContentType", System.Data.OleDb.OleDbType.VarChar,50).Value = UpFile.ContentType;OleDbCmdObj.Parameters.Add("@ImageDescription", System.Data.OleDb.OleDbType.VarChar,100).Value = txtDescription.Text;OleDbCmdObj.Parameters.Add("@ByteSize", System.Data.OleDb.OleDbType.VarChar,100).Value = UpFile.ContentLength;Con.Open();OleDbCmdObj.ExecuteNonQuery();Con.Close();
    以上代码中,首先建立了一个到SQL Server的数据库链接;然后,建立了一个数据插入的OleDbCommand,注意一点,在建立这个OleDbCommand的时候,我们这里没有使用“@”标志而是使用“?”来代替输入变量。另外,对于SQL.NET而言,因为其提供了一个SqlDbType.Image的数据类型,该类型对数据限制大小为:2,147,483,647,相信绝大部分用户不会上传这样大的文件,因此,可以直接使用以下语句:MySqlCmd.Parameters.Add("@Image", SqlDbType.Image).Value = FileByteArray而在OleDb.NET中,是没有这样类型定义的,我们只能使用OleDbType.Binary类型,这样,对于上传的限制就是8000字节,如果上传文件大于该限制,就会出错,因此,在使用OleDb.NET的时候,我们就必须很清楚的设置上传文件的大小,代码如下:OleDbCmdObj.Parameters.Add("@Image", System.Data.OleDb.OleDbType.Binary, FileLength).Value = FileByteArray
      

  7.   

    存图片:
    SqlConnection conn=new SqlConnection(@"Persist Security Info=False;initial catalog=picDataBase;data source=JAN;user id=sa;");
    conn.Open();
    SqlCommand cmd=new SqlCommand("insert into picb values ('@i')",conn);
    byte[] ib=new Byte[60000];
    FileStream fs=new FileStream(@"E:\wrooot\insert\image\boot1.gif",FileMode.Open ,FileAccess.Read );
    fs.Read(ib,0,60000);
    cmd.Parameters.Add("@i",SqlDbType.Image,(int)fs.Length);
    cmd.Parameters["@i"].Value=ib;
    cmd.ExecuteNonQuery();
    conn.Close();
      

  8.   

    看看这篇文章:
    http://blog.csdn.net/kwklover/archive/2004/06/22/22715.aspx
      

  9.   

    Oracle表PictureManager的结构为:PicDate date,Unit varchar2(40),Class varchar2(16),PicName varchar2(30),Pic blob ,yielder varchar2(30),yieldDate date,other varchar2(100) 。private void Button1_Click(object sender, System.EventArgs e)
    {
      Button1是打开本地图片的按钮,单击Button1时怎么实现弹出打开文件的对话框?
    }//下面是数据上传的代码,可以这样做吗,其中存在什么问题,还缺什么?
    private void Button2_Click(object sender, System.EventArgs e)
    {
      HttpPostedFile UpFile=UP_FILE.PostedFile;
      FileLength = UpFile.ContentLength;
      try 
      {
        if (FileLength == 0){
          Response.Write("<script>alert('请选择上传的图片!')</script>");
        } 
        else {
           //将输入控件中的数据存储到数组元素中。
           string [] picinfo=new string[8];
           picinfo[0]=TextBox1.Text; //PicDate 字段
           picinfo[1]=TextBox2.Text; //Unit 字段
           picinfo[2]=DropDownList1.SelectedItem.Text.ToString();//Class 字段
           picinfo[3]=TextBox3.Text; //PicName 字段
           picinfo[4]=?              //Pic 字段
           picinfo[5]=TextBox5.Text; //yielder 字段
           picinfo[6]=TextBox6.Text; //
           picinfo[7]=TextBox7.Text;
           dataSet11.Tables["PictureManager"].Rows.Add(picinfo);
           oracleDataAdapter1.Update(dataSet11,"PictureManager");
           oracleConnection1.Close();
           Response.Write("<script>alert('图片上传成功!')</script>");
        }
      } 
      catch (System.Exception ex){
        Response.Write(ex.Message.ToString());
      }
    }
      

  10.   

    难道没人能顶单击Button1来实现弹出打开文件的对话框?
      

  11.   

    <input type="file" id="fileImg">
    use  request("fileImg").inputstream
    then store the stream into your database
      

  12.   

    做过SQLSERVER上的没有做过ORACLE的
      

  13.   

    我也是,做过sql,没做过oracle
      

  14.   

    http://www.tn99.com/myblog/blogview.asp?logID=172&cateID=3
    希望对楼主有点帮助
      

  15.   

    我写的   SQL数据库   按钮事件如下:
    private void Button1_Click(object sender, System.EventArgs e)
    {
    HttpPostedFile  UpFile  =  UP_FILE.PostedFile;
    FileLength  =  UpFile.ContentLength; 
    try
    {
    if (FileLength == 0)
    {
    LabelMessage.Text = "请选择你要上传的图片!";
    }
    else
    {
    Byte[]  FileByteArray  =  new  Byte[FileLength];      
    Stream  StreamObject  =  UpFile.InputStream;  
    string filename = UpFile.FileName;
    StreamObject.Read(FileByteArray,0,FileLength);  

    string datastr = "server=192.168.0.207;user id=sa;password=999;database=Snow;";

    string SqlCmd3 = "select count(ImageID) from ImageStore where FileName ='"+filename.ToString()+"'"; 
    SqlConnection  Con3  =  new  SqlConnection(datastr); 
    SqlCommand Cmd3 = new SqlCommand(SqlCmd3,Con3);
     Con3.Open();
    int num = (int)Cmd3.ExecuteScalar();
    Con3.Close();
    if(num>0) 
    {
    Page.RegisterStartupScript("check", "<script>alert('数据库中已存在相同名称的图片!请另选一张!');</script>");
    }
    else
    {
    String  SqlCmd  =  "INSERT  INTO  ImageStore  (ImageData,  ImageContentType,  ImageDescription,  ImageSize,FileName)  " 
    +"VALUES  (@Image,  @ContentType,  @ImageDescription,  @ImageSize,@FileName)";  
    SqlConnection  Con  =  new  SqlConnection(datastr);  
    SqlCommand Cmd = new SqlCommand(SqlCmd,Con);
    Cmd.Parameters.Add("@Image",SqlDbType.Binary,  FileLength).Value  =  FileByteArray;  
    Cmd.Parameters.Add("@ContentType",  SqlDbType.VarChar,50).Value  =  UpFile.ContentType; 
    Cmd.Parameters.Add("@ImageDescription",  SqlDbType.VarChar,200).Value  =  this.TextBox1.Text;    
    Cmd.Parameters.Add("@ImageSize",  SqlDbType.BigInt,8).Value  =  UpFile.ContentLength;  
    Cmd.Parameters.Add("@FileName",  SqlDbType.VarChar,1000).Value  =  UpFile.FileName;
    Con.Open();  
    Cmd.ExecuteNonQuery();  
    Con.Close(); String  SqlCmd2  =  "select ImageID from ImageStore where FileName ='"+filename.ToString()+"'"; 
    SqlConnection  Con2  =  new  SqlConnection(datastr);  
    SqlCommand Cmd2 = new SqlCommand(SqlCmd2,Con2);  
    Con2.Open();
    string ID = Cmd2.ExecuteScalar().ToString();
    Con2.Close();
    this.LabelMessage.Text = "编号为"+ID+"的图片已经成功上传!";
    this.BindTP();
    }
    }
    }
    catch(Exception  ex)
    {
    this.LabelMessage.Text = ex.Message.ToString();  
    }
    }