Rendering an Image from a blob using an httpHandler
So I've written before on how to upload a file to a blob in Oracle, but I haven't written about how to retrieve it. In this post I am specifically talking about images in this post but you can change out the MIME type and it would work the same.I am going to show you how to use an IHTTPHandler in order to show your image. I used this for reference.
So add to your project a new generic handler. It will pre-populate a few methods, and basically you just add to them. See my code below
So after you have added this to your project you are ready to use it. There are many ways to do this, but I used an .Net image control. Either in the code behind or at design time, I defined the URL in the code behind when I bound the rest of the stuff on my page, but basically all you to have to do is link to the HttpHandler, and give it a query string with the ID you want. Example below
Public Class ImageHandler
Implements System.Web.IHttpHandler
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
If context.Request.QueryString("id") IsNot Nothing Then
Dim id As Integer = CInt(context.Request.QueryString("id"))
Dim blob As Byte()
blob = getImage(id)
If blob IsNot Nothing Then
context.Response.Clear()
context.Response.ContentType = "image/pjpeg"
context.Response.BinaryWrite(blob)
context.Response.End()
End If
End If
End Sub
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
Private Function getImage(ByVal ID As Integer) As Byte()
'Select blob from the db
End Function
End Class
Me.imgProductImage.ImageUrl = "~/imagehandler.ashx?id=" & D
Labels: .NET
posted by Tom Becker at
1/14/2009
|
1 Comments
![]()
