Search Persits Software Knowledge Base

 

Article PS01050249

HOWTO: Displaying images stored in the database as blobs

Problem Description

This article explains how to display an image stored in a database as a BLOB.

Solution

All major database management systems are capable of storing images (and any other binary files for that matter) in table fields, the same way as they store text or numeric data.

To retrieve and display an image from a database table, no third-party component is necessary. You can use the ADO Recordset object with the built-in method Response.BinaryWrite.

On your web page, place an <IMG> tag pointing to another ASP script, as follows:

<IMG SRC="downloadscript.asp?id=value">

A URL parameter is optional and can be used to pass the ID or name of an image to the download script.

The file downloadscript.asp , which must not contain any HTML tags, may look similar to this:

...
Set RecSet = Server.CreateObject("ADODB.Recordset")
RecSet.Open "select image_blob from image_table where id =" & Request("id"), Conn, 2, 3

If Not RecSet.EOF Then
   Response.ContentType = "image/jpeg"
   Response.BinaryWrite RecSet("image_blob")
End If

In some cases depending on the data types involved, you may need to use RecSet("image_blob").Value to access the image data.

Download a sample MS Access 2000 database containing two JPEG images, and two ASP files, runme.asp and helper.asp, that demonstrate this technique (109 KB).

Created: 2/8/2002 5:19:33 AM
Last Modified: 8/4/2003 3:14:56 PM

Copyright © Persits Software, Inc. 1998 - 2018
For technical support, write to support@persits.com.