DBPix Sample Source Code: getimage.asp Back to sample
<% @ LANGUAGE=VBScript %>
<% Option Explicit %>
<!--#include file="config.inc" -->
<%
On Error Resume Next
Response.Clear
Response.Buffer = TRUE
' If client sends an If-Modified-Since request return 304 Not Modified, since
' changes to an image result in a different unique url (by incrementing the version parameter).
Dim strIfModified
strIfModified = Request.ServerVariables("HTTP_IF_MODIFIED_SINCE")
If strIfModified <> "" Then
Response.Status = "304 Not Modified"
Response.End
Else
Dim cn
Dim rs
Dim SQL
Dim QueryId
Dim ItemId
Dim ImageVersion
QueryId = Request("query")
ImageVersion = Request("version")
ItemId = Request("itemid")
If (ImageVersion <> "" AND ItemId <> "" AND QueryId <> "") Then
Select Case QueryId
Case 1 ' Main Image
SQL = "SELECT tItems.Image AS TheImage, tItems.Id, tItems.ImageVersion " &_
"FROM tItems " &_
"WHERE ((tItems.Id=" & ItemId & ") AND (tItems.ImageVersion=" & ImageVersion & "))"
Case 2 ' Thumbnail
SQL = "SELECT tItems.Thumbnail AS TheImage, tItems.Id, tItems.ImageVersion " &_
"FROM tItems " &_
"WHERE ((tItems.Id=" & ItemId & ") AND (tItems.ImageVersion=" & ImageVersion & "))"
End Select
Set cn = Server.CreateObject("ADODB.Connection")
cn.Open GetConnectString
Set rs = cn.Execute(SQL)
If rs.EOF OR rs.BOF Then
Response.Status = "404 Not Found"
Else
' Set expiration headers to promote caching of the image, and set ContentType header to jpeg image
Response.cachecontrol = "public"
Response.Expires = 524160
Response.AddHeader "Last-modified","Mon, 01 Sep 1997 01:03:33 GMT"
Response.ContentType = "image/jpeg"
' Write image data back to client. Because MIME type is set to image/jpeg the browser will render as picture
Response.BinaryWrite rs("TheImage").GetChunk(rs("TheImage").ActualSize)
End If
' Cleanup:
rs.Close
Set cn = Nothing
Set rs = Nothing
Else
Response.Status = "404 Not Found"
End If
Response.End
End If
%>
Back to sample
|