HOWTO: Displaying Images Stored in a BLOB FieldID: Q173308
|
Using Active Server Pages (ASP), you can view images stored in BLOB (Binary Large Object) fields in your Internet browser. This article provides information on how to display a GIF image stored in the Microsoft SQL Server sample database table pub_info.
Most Internet browsers support the displaying of GIF and JPEG images. To
display an image, the browser requests the image from a Web server. The
server passes the image to the browser as an HTTP transaction with an HTTP
header containing an MIME type of IMAGE/GIF or IMAGE/JPEG. You can simulate
this behavior with Active Server Pages.
The following example constructs an HTTP header for an image and then uses
the binary information from an image field in SQL Server to provide a GIF
to the browser.
FILE: SHOWIMG.ASP
<%@ LANGUAGE="VBSCRIPT" %>
<%
' Clear out the existing HTTP header information
Response.Expires = 0
Response.Buffer = TRUE
Response.Clear
' Change the HTTP header to reflect that an image is being passed.
Response.ContentType = "image/gif"
Set cn = Server.CreateObject("ADODB.Connection")
' The following open line assumes you have set up a System DataSource
' by the name of myDSN.
cn.Open "DSN=myDSN;UID=sa;PWD=;DATABASE=pubs"
Set rs = cn.Execute("SELECT logo FROM pub_info WHERE pub_id='0736'")
Response.BinaryWrite rs("logo")
Response.End
%>
<HTML>
<HEAD><TITLE>Display Image</TITLE></HEAD>
<BODY>
This page will display the image New Moon Books from a SQL Server 6.5
image field.<BR>
<IMG SRC="SHOWIMG.ASP">
</BODY>
</HTML>
NOTE: The ASP script assumes that the image field (BLOB Data) in the SQL Server table contains a raw GIF image. Internet browsers assume that raw GIF or JPEG data follow the HTTP header. If any extraneous information is contained in the BLOB data, this will be passed by this script, and the image will not display properly. This becomes important when you realize that most methods of placing images into BLOB fields place extra information in the form of headers with the image. Examples of this are Microsoft Access and Microsoft Visual FoxPro. Both of these applications save OLE headers in the BLOB field along with the actual binary data.
For the latest Knowledge Base artices and other support information on Visual InterDev and Active Server Pages, see the following page on the Microsoft Technical Support site:
http://support.microsoft.com/support/vinterdev/(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Paul Enfield, Microsoft Corporation.
Additional query words:
Keywords : kbASP kbASPObj kbVBScript kbVisID kbVisID100 kbGrpASP
Version : WINDOWS:97,97sp1,97sp2; winnt:
Platform : WINDOWS winnt
Issue type : kbhowto
Last Reviewed: May 27, 1999