When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Technology Jobs
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs

The 4 Guys Present: ASPFAQs.com

Jump to a FAQ
Enter FAQ #:
..or see our 10 Most Viewed FAQs.

4GuysFromRolla.com : ASP FAQS : Databases, General


How can I display images that reside in my database through an ASP page?

[Print this FAQ]

Answer: 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.
3.) Use Response.BinaryWrite to output the contents of the image.

The 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 Response.ContentType to image/gif.

The 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:
<img src="ShowPicture.asp">

For more information on both of these processes, be sure to read: Displaying Images that Reside in a Database

Happy Programming!

FAQ posted by Scott Mitchell at 11/12/2001 12:49:12 PM to the Databases, General category. This FAQ has been viewed 110,298 times.

Do you have a FAQ you'd like to suggest? Suggestions? Comments? If so, send it in! Also, if you'd like to be a FAQ Admin (creating/editing FAQs), let me know! If you are looking for other FAQs, be sure to check out the 4Guys FAQ and Commonly Asked Messageboard Questions!

Most Viewed FAQs:

1.) How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency. (761643 views)
2.) I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem? (207777 views)
3.) How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset? (202549 views)
4.) How can I quickly sort a VBScript array? (196039 views)
5.) How can I find out if a record already exists in a database? If it doesn't, I want to add it. (156019 views)
6.) How do I display data on a web page using arrays instead of Do...While...MoveNext...???... (152331 views)
7.) When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or... (140381 views)
8.) For session variables to work, must the Web visitor have cookies enabled? (110162 views)
9.) Can I send emails without using CDONTS? (107083 views)
10.) How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query? That is, if the user selects 3 answers, how can I construct a query that looks for all 3? (106308 views)
Last computed at 9/17/2007 3:22:00 AM

Software Developer / Programmer - Distributed Systems (NYC)
Next Step Systems
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article