Display Image From Database

The ASPSmith Articles, ASPAlliance.com

Display Image From Database

By Steven Smith
http://www.aspalliance.com/stevesmith/articles/imagequery.asp
[Example]

It is possible to store images in a database using a number of third party tools or a custom built component. Once you have the images stored in your database, however, displaying them requires the use of a simple ASP page. In order to view the image, you should use the standard HTML IMG tag, and reference the ASP page listed below (called getpicture.asp), and pass the page the ID of the image you wish to display in a querystring, as follows:

	<IMG SRC="getpicture.asp?ID=1">

The code required for this page is very simple. A database connection is established and the database is queried for the record with the passed in ID. Then, the document's content-type is set to "image/gif" to let the browser know to interpret the coming data as an image. Finally, the Response.Binarywrite method is used to output the binary data from the database. Note how this is actually used in an IMG tag of an HTML page!

Note that it is almost always better for performance to store images in the file system and just path information in the database! However, this article is devoted to How, not Why or When, to retrieve images that have been stored in a database.

   <% OPTION EXPLICIT %>
   <%
   On Error Resume Next
   Dim sql
   Dim rs
   Dim conn
   Dim strID
   
   'Get querystring parameter
10   strID = Request("ID")
11   If strID = "" Then strID = 0
12   
13   'Instantiate Objects
14   Set conn = Server.CreateObject("ADODB.Connection")
15   Set rs = Server.CreateObject("ADODB.Recordset")
16   
17   'Open connection
18   conn.Open Application("aspdb_ConnectionString")
19   
20   'Get the specific image based on the ID passed in a querystring
21   sql = "SELECT f.binary_data " & _
22    " FROM t_file f " & _
23    " WHERE f.file_id = " & strID
24   rs.Open sql, conn
25   
26   'If no record found, end.
27   if rs.eof then
28    rs.Close
29    Response.End
30   end if
31   
32   'Display the contents of the record as an image
33   Response.ContentType = "image/gif"
34   Response.BinaryWrite(rs("binary_data"))
35   
36   'Clean up
37   rs.Close
38   conn.Close
39   set rs = Nothing
40   set conn = Nothing
41   %>

If you "Try this Script" you will have to give it an ID (?ID=1). Here is a reference to the above file, with an ID:
ASPAlliance Logo
and here is a link to the file itself:
getpicture.asp

You can use AspUpload to load the image into the database, as this sample demonstrates:

   <% OPTION EXPLICIT %>
   <%
   'On Error Resume Next
   Dim objUpload
   
   Set objUpload = Server.CreateObject("Persits.Upload")
   
   'The t_file table has two columns, a varchar
   'name column and an image binary_data column.
10   objUpload.ToDatabaseEx Server.MapPath("/images/asplogobevel3.gif"), _
11    application("aspdb_connectionstring"), _
12    "INSERT INTO t_file (name,binary_data) values ('AspAlliance Icon',?)"
13   set objUpload = Nothing
14   %>

15   File uploaded!
16   

Complete instructions for uploading the image using AspUpload are available on the product's website. Note that a common problem with this technique is user permissions, so be sure to check the website's FAQ for information on how to set permissions properly.