DBPix Sample Source Code: getimage.asp Back to sample
<% @ LANGUAGE=VBScript %>
<% Option Explicit %>
<!--#include file="config.inc" -->
<%
' This file gets the requested image from the database and sends it back to the browser.
' By setting up the MIME headers the data stream will be interpreted by the browser as an
' image, just as if it had been sent a regular image file.
' The referring web page (i.e. the one that displays the image) generates an image tag such as:
' <img src="getimage.asp?field=Image&imgid=12" width="163" height="140">
Dim strRecordId ' Id of image to retrieve (from querystring)
Dim strImgField ' Name of database field containing image (from querystring)
Dim strSQL ' String to hold SQL to retrieve image
Dim oConn ' Connection object
Dim oRs ' Recordset object
' Setup HTTP Header Information so that the browser interprets the returned data as an image
Response.Buffer = TRUE
Response.ContentType = "image/jpeg"
' Open a connection to the database (connection string from include file "config.inc")
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open GetConnectString
' Get the requested image id from the request object
strRecordId = Request("imgid")
' Get the name of the field that contains the image we want (eg the Thumbnail or Main image field),
strImgField = Request("field")
' Check that a valid id was passed
If (strRecordId <> "") Then
' Prepare the Query
strSQL = "SELECT Id, " & strImgField & " FROM tPhotos WHERE Id = " & strRecordId
' Execute the query and get the returned recordset
Set oRs = oConn.Execute(strSQL)
' Make sure we have a record
if NOT (oRs.EOF AND oRs.BOF) then
' Make sure that the image field contains some data
if oRs(strImgField).ActualSize > 0 then
' Write the Data back to client. Because MIME type is set to image/jpeg the browser
' will automatically render as an image without needing controls, applets etc.
Response.BinaryWrite oRs(strImgField).GetChunk(oRs(strImgField).ActualSize)
end if
end if
oRs.Close
Set oRs = nothing
end if
Set oConn = nothing
Response.End
%>
Back to sample
|