||This is a fairly commonly asked question on ASPMessageboard.com. Essentially, you can have images stored in a database in one of two formats: as a text field specifying a URL from where the image can be found; or as a binary field stored within the database.|
There are times when each approach makes sense. Since we are dealing with the Web, oftentimes it makes more sense to use the former approach, to simply store the URLs to the images as opposed to actually storing the binary contents of the image in the database. Such practices will keep your overall database size small and make displaying images a breeze (just need a simple
IMG tag whose
SRC attribute uses the database value). The disadvantage with this approach is that is decouples the images from the database. This could be a problem if your database is used on multiple servers scattered throughout the country or world, and the images are on a computer that is, perhaps, only reachable through a certain location, perhaps an intranet of some kind.
The other option is to store the image contents into the database as a binary field. As I summarized before, this oftentimes greatly increases the overall size of your database, but is nice because all of the data for your database exists in one location, as opposed to having the images residing separately.
So how does one display an image from a database via an ASP page? Using the first technique, assume that your table had a column called
ImageUrl, that contained a fully qualified URL to the image file, such as:
http://www.yourserver.com/images/hat.gif, or whatever. Then, to display the image via an ASP page, you'd simply need to display an
IMG tag like so:
Response.Write("<img src="" & Recordset("ImageUrl") & """>")
This simply outputs an
IMG tag with the
SRC set to the
ImageUrl column in the database table.
Displaying an image whose binary contents resides in the database itself is a triffle more difficult. For this scenario it is assumed that there is a BLOB column named
Picture that stored the actual bits for the image. In your ASP page you need to, essentially, do three things:
1.) Read in the image column into a Recordset
2.) Set the
ContentType property of the Response object to the image's filetype.
Response.BinaryWrite to output the contents of the image.
Response.ContentType property sends a bit of information to the browser informing the browser how to interpret the binary information it will be receiving. So, if you are storing a gif image in the database, you should set
BinaryWrite method allows the Response object to send binary information as opposed to textual information. You'll need to use this to blast the image's bits to the browser. A high-level look at the code needed to display an image whose bits reside in the database itself can be seen below:
'Read in the image from the database
Set objRS = objConn.Execute("SELECT Picture FROM TableName WHERE ID = 1")
'Set the ContentType to image/gif
Response.ContentType = "image/gif"
'Send the binary bits to the browser
That's all there is to it! Realize that you cannot display both binary information and textual information from this ASP page, you can only send the image down the wire. If you want to include this on an ASP page with textual information, you will need to reference the ASP page that displays the image via an
IMG tag in the ASP page that you wish to display textual information alongside the picture. For example:
This is a picture:
For more information on both of these processes, be sure to read: Displaying Images that Reside in a Database