|To display images from the database over the web 2 steps are required:|
Firstly an image tag must be generated in the page where you want to see the image.
This tag will look something like the following:
<img src="getimage.asp?id=13" width="163" height="140">
typical code to generate this could be:
<img src="getimage.asp?id=<%=rs("ImageId")%>" width="<%=rs("ImageW")%>" height="<%=rs("ImageH")%>">
Note that we have stored the image dimensions so that they are available when we generate the image tag. Including them improves display by allowing the browser to layout the page correctly before all the images have been retrieved.
When the browser requests the image the "getimage.asp" file is run on the server. This file extracts the requested image from the database and sends it back to the browser, looking just like a file had been served from disk. "getimage.asp" contains the following code:
<% @ LANGUAGE=VBScript %>
<% Option Explicit
' Configure caching. In a production system you should configure this accordingly
Response.Expires = 0
Response.Buffer = TRUE
' Set the appropriate MIME type in the HTTP Header Information.
Response.ContentType = "image/jpeg"
' Connect to the database-substitute your path or connect string
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &_
' Get the image id from the request parameters and generate the SQL statement
SourceID = Request("id")
If (SourceID <> "") Then
SQL = "SELECT * FROM tImages WHERE Id = " & SourceID
Set oRs = oConn.Execute(SQL)
if NOT (oRs.EOF AND oRs.BOF) then
PicSize = oRs("Image").ActualSize
if PicSize > 0 then
' Write the Image Data to the client.
Set oRs = Nothing
Set oConn = Nothing
Check the Samples area in Support for downloadable demos that use this technique.