Downloading Images and Retrieving from DB2

H! I am trying to upload an image to a DB2 database. Image size - JPG (6.76 kb - 6924 bytes).

There is a BLOB field of 1048576 in the database table.

My code for inserting an image is as follows:

If fileup.PostedFile IsNot Nothing AndAlso fileup.PostedFile.FileName <> "" Then
   Dim imagesize As Byte() = New Byte(fileup.PostedFile.ContentLength - 1) {}
   Dim uploadedimage1 As HttpPostedFile = fileup.PostedFile

   uploadedimage1.InputStream.Read(imagesize, 0, CInt(fileup.PostedFile.ContentLength))

   Dim uploadedimage2 As New OleDbParameter("@Image", OleDbType.VarBinary, imagesize.Length)
   uploadedimage2.Value = imagesize

   Dim cmd As New OleDbCommand()
   cmd.CommandText = "INSERT INTO xxx_TBL(x, IMAGE)  VALUES (?, ?)"
   cmd.Parameters.Add(x)
   cmd.Parameters.Add(uploadedimage2)

   cmd.Connection = clsDatabase.Open_DB()
   Dim result As Integer = cmd.ExecuteNonQuery()
   If result > 0 Then
      Return True

The image is inserted into the database.

The code for retrieving an image from a database in a DataTable, which is then bound to a GridView to display on a web page, looks like this:

Dim cmd As New OleDbCommand()

cmd.CommandText = "SELECT x, IMAGE FROM xxx_TBL WHERE y = 1" 

cmd.Connection = clsDatabase.Open_DB()
Dim dReader As OleDbDataReader
dReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Comments", GetType(String)))
dt.Columns.Add(New DataColumn("Picture", GetType(Bitmap)))

Do While (dReader.Read())
   Dim dr As DataRow = dt.NewRow()

   dr("Comments") = dReader(0).ToString
   Dim imageobj = dReader(1)
       Using ms As New System.IO.MemoryStream
            Dim bm As Bitmap
            Dim bytearray = DirectCast(imageobj, Byte())
            ms.Write(bytearray, 0, bytearray.length)
            bm = New Bitmap(ms)
            dr("Picture") = bm
       End Using

    dt.Rows.Add(dr)
Loop

GridView1.DataSource = dt
GridView1.DataBind()

x comes from the database accurately and is displayed. However, I do not have an image - just a small “missing photo (red cross on white)”.

BLOB 8192. "TEST" ( ) 13848 . , - , DB2 / , . - ? ?

0
1

DB2, comments, image . , image , , , comments. HTTP- , <img src=myImg.png> .

dt , , HttpHandler , .

<img> GivdView1.

<img src="MyHttpHandler.ashx?id=1"/>, , ID .

Generic Handler Visual Studio MyHttpHandler.ashx , :

public class MyHttpHandler : IHttpHandler
{    
    public void ProcessRequest(HttpContext context)
    {
        string sid = context.Request.QueryString["id"];
        int id = -1;
        if(int.TryParse(sid, out id) && id > 0){
            cmd.CommandText = "SELECT IMAGE FROM xxx_TBL WHERE ID=" + id.ToString() + ";";
            byte[] img = dr["IMAGE"];
            context.Response.ContentType = "image/png";
            context.Response.BinaryWrite(img);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

, , gridview html, HTTP- , MyHttpHandler.ashx .

0

All Articles