HOWTO: Displaying Images Stored in a BLOB Field

Last reviewed: December 11, 1997
Article ID: Q173308
The information in this article applies to:
  • Microsoft Active Server Pages, version 1.0b
  • Microsoft Visual Studio, versions 97, 97sp1, 97sp2

SUMMARY

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.

MORE INFORMATION

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
   %>

This script only displays an image on the screen. If you wish to display an image from an HTML or ASP document you must refer to this script in an image tag. For example, if you wished to display this image with a caption describing it, you might use the following HTML page:

   <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.

This technique can also be applied to other types of binary data, not just graphics. The browser needs to know what type of content is being presented. Do this by specifying the proper mime type in the Response.ContentType variable. For example, if you wanted to view a word document, you would set the ContentType = "application/msword".

REFERENCES

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
Keywords          : VIASP
Technology        : kbInetDev
Version           : WINDOWS NT:1.0b,97,97sp1,97sp2
Platform          : NT WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: December 11, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.