Click here to Skip to main content
15,891,431 members
Articles / Programming Languages / ASP
Article

Display Images from Database in ASP

Rate me:
Please Sign up or sign in to vote.
4.44/5 (41 votes)
30 May 20041 min read 441.2K   6.4K   50   53
This article describes how to display images which are stored in database in either SQL Server or MS Access.

Introduction

This article describes how to display images which are stored in a database. You can use <IMG> tag to display images stored in the database by calling another ASP page in SRC attribute of <IMG> tag.

For example:

HTML
<IMG SRC="ShowPicture.asp?PhotoId=1">

where PhotoId is the ID stored in the database.

Using the code

Following are the steps which needs to be executed:

  1. Create table Users in MS-Access or SQL server.
  2. Create ShowPicture.asp page.
  3. Call this page using <IMG> tag wherever required.

Create table with the following structure:

Table name: Users

  • user_id (AutoNumber)
  • user_name (Text)
  • user_photo (Ole Object - For MS-Access, and Image data type for SQL server).

Code

ShowPicture.asp is used to display images. You need to pass user ID in querystring, and for that user ID, image will be displayed. Following is the code:

VBScript
'Declare Variables..
   Dim sql
   Dim rs
   Dim conn
   Dim userID,str

  userID = Request("PhotoId")
  If userID = "" Then userID = 0

  'Instantiate Objects
  Set conn = Server.CreateObject("ADODB.Connection")
  Set rs = Server.CreateObject("ADODB.Recordset")

  'Open connection
  Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & Server.MapPath("data.mdb")

  'Get the specific image based on the ID passed in a querystring
   str = "SELECT user_photo FROM users where user_id =" & userID
   rs.Open str, conn,3,3
   if rs.eof then 'No records found
       Response.End
   else 'Display the contents
       Response.ContentType = "image/gif"
       Response.BinaryWrite(rs("user_photo"))
   end if

  'destroy the variables.
  rs.Close
  conn.Close
  set rs = Nothing
  set conn = Nothing

Please note that "Response.contentType" will depend on the type of content you would like to display. For example, to display jpg image, following will be the code:

VBScript
Response.ContentType = "image/jpg"

To use above page for displaying images, following is the example:

HTML
<IMG SRC="ShowPicture.asp?PhotoId=1">
<IMG SRC="ShowPicture.asp?PhotoId=2">

Upload Image

Following example shows how to upload images in database by using ASPSmartUpload component. More information about this component can be found here.

Following is the code to upload image:

HTML
<FORM METHOD="POST" ACTION="saveFile.asp" 
        ENCTYPE="multipart/form-data" NAME="UploadForm">
   <center>
   Employee Name : <INPUT TYPE="TEXT" NAME="USERNAME" SIZE="30"><br>
   <INPUT TYPE="FILE" NAME="UPLOADFILE1" SIZE="50">
   </center>
   <BR>
   <center><INPUT TYPE="SUBMIT" VALUE="Upload" 
             id=SUBMIT1 name=SUBMIT1></center>
</FORM>

Code to save the image in the database:

VBScript
Dim myupload
   'declare variables..
   intCount=0
        
    ' Create Upload Component
    Set myupload = Server.CreateObject("aspSmartUpload.SmartUpload")
    myupload.Upload

    'Create Connection
    Set Conn = Server.CreateObject("ADODB.Connection")
    Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ 
              "Data Source=" & Server.MapPath("data.mdb")
   
    Set Rs = Server.CreateObject("ADODB.recordset")
    Rs.Open "SELECT user_id,user_name,user_photo FROM users", Conn,3,3

    'Select each file
    For each file In myupload.Files
        If not file.IsMissing Then 'Check for missing file
            'Add the current file in database
            Rs.AddNew
            file.FileToField Rs.Fields("user_photo")
            Rs("user_name") = myupload.Form("USERNAME")
            Rs.Update
            intCount = intCount + 1
        End If
    Next
    Response.Write(intCount & " file(s) uploaded.")
    'Destroy the objects...

Comments

It is always better to store images in the file system rather than storing in the database. Only image path can be stored in the database. However, storing images in the database depends on requirements and needs.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalrenaming files. help! Pin
rbeenlio10-Apr-05 17:02
rbeenlio10-Apr-05 17:02 
GeneralRe: renaming files. help! Pin
rbeenlio10-Apr-05 19:22
rbeenlio10-Apr-05 19:22 
GeneralRe: renaming files. help! Pin
Om Prakash Pant31-May-05 4:02
Om Prakash Pant31-May-05 4:02 
QuestionHow to refresh page when item selected in combo box Pin
crazy_guy24-Mar-05 6:49
crazy_guy24-Mar-05 6:49 
QuestionHow to download file from database? Pin
Gundars Alksnis18-Feb-05 2:51
Gundars Alksnis18-Feb-05 2:51 
AnswerRe: How to download file from database? Pin
Om Prakash Pant18-Feb-05 7:05
Om Prakash Pant18-Feb-05 7:05 
GeneralRe: How to download file from database? Pin
Gundars Alksnis19-Feb-05 8:31
Gundars Alksnis19-Feb-05 8:31 
GeneralRe: How to download file from database? Pin
Member 19333043-May-05 13:07
Member 19333043-May-05 13:07 
Hello,

I have not been able to get the sample to work with SQL 2000. I can upload to the database, but i cannot use the .downloadField to save the file from the database.

If I use this:

Response.ContentType = "application/msword"
Response.BinaryWrite(oRs("FILE"))Ron


It will load my file into a new window though. The name of the document defaults to the asp page name however.

Do you know how to get the sampole coade to work with SQL, or how I can get the BinaryWrite version to have the correct file name?

I am storing the contentType and original file name in the database along with the file itself. It is used to store code snippits and business rule docement changes.

Thanks in advance..


Ron
GeneralRe: How to download file from database? Pin
yunan70731-May-05 17:24
yunan70731-May-05 17:24 
GeneralDisplay image when a click occur Pin
Anonymous20-Oct-04 20:15
Anonymous20-Oct-04 20:15 
GeneralRe: Display image when a click occur Pin
Om Prakash Pant26-Oct-04 23:51
Om Prakash Pant26-Oct-04 23:51 
Generallulu Pin
lulu505022-Sep-04 11:48
lulu505022-Sep-04 11:48 
QuestionWhat about multi part Tiff's Pin
dibblm-Ohio1-Jun-04 4:41
dibblm-Ohio1-Jun-04 4:41 
Generaljust a note... Pin
l a u r e n31-May-04 8:09
l a u r e n31-May-04 8:09 
GeneralRe: just a note... Pin
LiborV2-Jun-04 10:01
LiborV2-Jun-04 10:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.