本文介绍了从 DB2 上传和检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喂!我正在尝试在 DB2 数据库中上传图像.图像大小为 JPG(6.76 kb - 6924 字节).

H! I'm trying to upload an image in a DB2 database.The image size is a JPG (6.76 kb - 6924 bytes).

数据库表有一个长度为 1048576 的 BLOB 字段.

The database table has a BLOB field of length 1048576.

我插入图片的代码如下:

My code to insert the 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 gets inserted into the Database.

将图片从数据库中取出,放入DataTable,然后绑定到GridView上显示在网页上的代码如下:

The code to get the image from the Database, into a DataTable, which is then bound to a GridView to display on the webpage is as follows:

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 来自数据库很好,并显示出来.但是,我没有得到图片 - 只是一个小的缺少图片(白底红十字)图标".

x comes from the Database fine, and is displayed. However, I get no picture - just a small "missing picture (red cross on white) icon".

检查数据库时,BLOB 字段中的图像长度为 8192.但是,将其复制到TEST"文件(无扩展名)时,大小为 13848 字节.我猜这可能是因为 DB2 读取/编码图像二进制文件的方式,但我不确定.有人可以强调错误的可能原因吗?有关使这项工作或调试的任何建议?

On checking the database, the length of the image in BLOB field is 8192. However, on copying it onto a 'TEST' File (no extension), the size was 13848 bytes. I'm guessing that could be because of the way DB2 reads/encodes the image binary, but I'm not sure.Could someone please highlight the possible causes of error? Any suggestions on making this work or debugging?

推荐答案

我对 DB2 不熟悉,但既然你可以显示 comments,你的 image 应该被正确阅读.假设您的 image 数据也已正确存储,您的代码不会在浏览器中显示它们,因为它们需要与 comments 稍有不同的注意事项.每个图像都需要对您的服务器发出额外的 HTTP 请求,因此您必须创建那些 <img src=myImg.png> 标签来代替图像持有者.

I'm not familiar with DB2 but since you are able to display comments, your image should be read properly. Let's assume your image data is also stored properly, your code will not display them in the browser because they need a little different care then comments. Each image needs to be an additional HTTP request to your server, so you'll have to create those <img src=myImg.png> tags in place of image holders.

在您的 dt 中,您不需要实际的图像数据,您只需要图像的 ID,因为您需要使用 HttpHandler 来检索图像,然后流式传输到浏览器.

In your dt, you don't need the actual image data, you just need the ID to the image because you'll need to use a HttpHandler to retrieve the image and then stream to browser.

首先,您需要在 GivdView1 的图像列中添加一个 <img> 标签.

first, you'll need a <img> tag in your GivdView1's image column.

那么,在数据绑定的时候,一定要更新成,当然ID值必须是动态的.

then, during data binding, make sure to update it to <img src="MyHttpHandler.ashx?id=1"/>, of course, the ID value must be dynamic.

现在,将一个新文件 Generic Handler 添加到您的 Visual Studio 项目并将其命名为 MyHttpHandler.ashx 并编写如下代码:

now, add a new file Generic Handler to your Visual Studio project and name it MyHttpHandler.ashx and code something like below:

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 会将每个图像流回浏览器显示.

Now, after gridview renders the html, the browser will make HTTP request for all those images and your MyHttpHandler.ashx will stream each image back for browser to display.

这篇关于从 DB2 上传和检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:25
查看更多