Microsoft KB Archive/173308: Difference between revisions

From BetaArchive Wiki
m (Text replacement - "<" to "<")
m (Text replacement - """ to """)
 
(One intermediate revision by the same user not shown)
Line 60: Line 60:
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.
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.
<pre class="codesample">  FILE: SHOWIMG.ASP
<pre class="codesample">  FILE: SHOWIMG.ASP
   <%@ LANGUAGE=&quot;VBSCRIPT&quot; %&gt;
   <%@ LANGUAGE="VBSCRIPT" %>
   <%
   <%
   ' Clear out the existing HTTP header information
   ' Clear out the existing HTTP header information
Line 68: Line 68:


   ' Change the HTTP header to reflect that an image is being passed.
   ' Change the HTTP header to reflect that an image is being passed.
   Response.ContentType = &quot;image/gif&quot;
   Response.ContentType = "image/gif"


   Set cn = Server.CreateObject(&quot;ADODB.Connection&quot;)
   Set cn = Server.CreateObject("ADODB.Connection")
   ' The following open line assumes you have set up a System DataSource
   ' The following open line assumes you have set up a System DataSource
   ' by the name of myDSN.
   ' by the name of myDSN.
   'Remember to change the following connection string parameters to reflect the correct values
   'Remember to change the following connection string parameters to reflect the correct values
   'for your SQL server.
   'for your SQL server.
   cn.Open &quot;DSN=myDSN;UID=<username&gt;;PWD=<strong password&gt;;DATABASE=pubs&quot;
   cn.Open "DSN=myDSN;UID=<username>;PWD=<strong password>;DATABASE=pubs"
   Set rs = cn.Execute(&quot;SELECT logo FROM pub_info WHERE pub_id='0736'&quot;)
   Set rs = cn.Execute("SELECT logo FROM pub_info WHERE pub_id='0736'")
   Response.BinaryWrite rs(&quot;logo&quot;)
   Response.BinaryWrite rs("logo")
   Response.End
   Response.End
   %&gt;
   %>
                 </pre>
                 </pre>
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:
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:
<pre class="codesample">  <HTML&gt;
<pre class="codesample">  <HTML>
   <HEAD&gt;<TITLE&gt;Display Image</TITLE&gt;</HEAD&gt;
   <HEAD><TITLE>Display Image</TITLE></HEAD>
   <BODY&gt;
   <BODY>
   This page will display the image New Moon Books from a SQL Server
   This page will display the image New Moon Books from a SQL Server
   image field.<BR&gt;
   image field.<BR>
   <IMG SRC=&quot;SHOWIMG.ASP&quot;&gt;
   <IMG SRC="SHOWIMG.ASP">
   </BODY&gt;
   </BODY>
   </HTML&gt;
   </HTML>
                 </pre>
                 </pre>
'''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.<br />
'''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.<br />
<br />
<br />
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 = &quot;application/msword&quot;.
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".


</div>
</div>

Latest revision as of 11:06, 21 July 2020

Knowledge Base


Article ID: 173308

Article Last Modified on 5/2/2006



APPLIES TO

  • Microsoft Active Server Pages 4.0
  • Microsoft Internet Information Server 4.0
  • Microsoft Internet Information Services 5.0
  • Microsoft Data Access Components 2.5
  • Microsoft Data Access Components 2.6
  • Microsoft Data Access Components 2.7



This article was previously published under Q173308

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.
   'Remember to change the following connection string parameters to reflect the correct values
   'for your SQL server.
   cn.Open "DSN=myDSN;UID=<username>;PWD=<strong password>;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
   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:

Keywords: kbhowto kbcodesnippet kbdatabase kbaspobj KB173308