Click here to Skip to main content
11,416,194 members (66,419 online)
Click here to Skip to main content

Display Images from Database in ASP

, 30 May 2004
Rate this:
Please Sign up or sign in to vote.
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:

<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:

 '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:

     Response.ContentType = "image/jpg"

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

     <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:

<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:

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

Share

About the Author

Om Prakash Pant
Web Developer
India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 Pin
manoj kumar choubey21-Jan-12 7:06
membermanoj kumar choubey21-Jan-12 7:06 
Question"Error" Invalid class string [modified] Pin
wahed210211-Jun-11 15:53
memberwahed210211-Jun-11 15:53 
GeneralI want to do this but with ASP reading from an MS Access 2007 Attachment field. Pin
Scottsr5-Apr-11 15:20
memberScottsr5-Apr-11 15:20 
GeneralMy vote of 5 Pin
Member 204452912-Feb-11 1:40
memberMember 204452912-Feb-11 1:40 
GeneralMy vote of 4 Pin
codemoreforless8-Aug-10 11:50
membercodemoreforless8-Aug-10 11:50 
GeneralRbmBinaryImage Pin
RMostafa26-Apr-09 22:49
memberRMostafa26-Apr-09 22:49 
GeneralI want to store the checkboxs and textboxs values in database - asp Pin
shivasusan29-Oct-08 23:28
membershivasusan29-Oct-08 23:28 
GeneralWith Sql but asp.net - simple with asp Pin
mayurmv5-Sep-08 18:24
membermayurmv5-Sep-08 18:24 
GeneralI want Code for serch data statewise from all state Pin
influxsam4-May-08 21:30
memberinfluxsam4-May-08 21:30 
GeneralAccess 2007/SQL Express 2005 and viewing images from ASP Pin
Jstncase16-Apr-08 10:10
memberJstncase16-Apr-08 10:10 
GeneralRe: Access 2007/SQL Express 2005 and viewing images from ASP Pin
Scottsr5-Apr-11 15:25
memberScottsr5-Apr-11 15:25 
GeneralThanks Pin
Cuchuk Sergey10-Feb-08 22:54
memberCuchuk Sergey10-Feb-08 22:54 
GeneralHi Can you suggest how to Upload Images in ASP.NET Pin
kodalis29-May-07 6:59
memberkodalis29-May-07 6:59 
GeneralRe: Hi Can you suggest how to Upload Images in ASP.NET Pin
Bilal Haider Asi1-Aug-07 5:22
memberBilal Haider Asi1-Aug-07 5:22 
AnswerRe: Hi Can you suggest how to Upload Images in ASP.NET Pin
tfzwgd1-Dec-09 17:44
membertfzwgd1-Dec-09 17:44 
QuestionNo duplicate files Pin
dlbdennis10-May-07 18:49
memberdlbdennis10-May-07 18:49 
Questionhow to put flash pgms Pin
Shyju.K18-Feb-07 20:42
memberShyju.K18-Feb-07 20:42 
General.jpg images from access database displayed as garble Pin
Member #38120469-Feb-07 4:18
memberMember #38120469-Feb-07 4:18 
GeneralDisplay all images Pin
brtakaram14-Dec-06 1:24
memberbrtakaram14-Dec-06 1:24 
GeneralRe: Display all images Pin
brtakaram19-Dec-06 7:48
memberbrtakaram19-Dec-06 7:48 
GeneralDisplay image along with text Pin
tonyg2smith18-Oct-06 7:02
membertonyg2smith18-Oct-06 7:02 
GeneralRe: Display image along with text Pin
rameshkumarp20-Feb-07 4:15
memberrameshkumarp20-Feb-07 4:15 
AnswerRe: Display image along with text Pin
tonyg2smith23-Feb-07 4:42
membertonyg2smith23-Feb-07 4:42 
QuestionUnable to Upload Image with aspSmartUpload component [modified] Pin
asprajesh16-Oct-06 1:13
memberasprajesh16-Oct-06 1:13 
Questionsmartupload component ? Pin
coolraazi11-Oct-06 1:04
membercoolraazi11-Oct-06 1:04 
AnswerRe: smartupload component ? Pin
hardik_7920-Nov-10 0:29
memberhardik_7920-Nov-10 0:29 
Generalimage path Pin
M1K3Y5-Jun-06 2:00
memberM1K3Y5-Jun-06 2:00 
Questionhow to download a file from database in asp? Pin
pusycat18-May-06 2:04
memberpusycat18-May-06 2:04 
GeneralNot Uploading to SQL Database Pin
in4_mus19-Mar-06 10:22
memberin4_mus19-Mar-06 10:22 
GeneralDisplay Images from Database in ASP when we activate sample.asp x box will appear instead of our picture 1 ,2 which was written by Om Prakash P Pin
alsadig19-Dec-05 5:02
memberalsadig19-Dec-05 5:02 
Generalwhen we activate sample.asp x box will appear instead of our picture 1 ,2 Pin
alsadig19-Dec-05 4:55
memberalsadig19-Dec-05 4:55 
Generalsample.asp Pin
alsadig19-Dec-05 4:52
memberalsadig19-Dec-05 4:52 
QuestionHow to update the images files from database? Pin
RSRT26-May-05 13:27
memberRSRT26-May-05 13:27 
AnswerRe: How to update the images files from database? Pin
Om Prakash P27-Jun-05 0:06
memberOm Prakash P27-Jun-05 0:06 
GeneralRe: How to update the images files from database? Pin
basg12-May-06 22:44
memberbasg12-May-06 22:44 
GeneralRe: How to update the images files from database? Pin
basg12-May-06 22:47
memberbasg12-May-06 22:47 
Generalrenaming files. help! Pin
rbeenlio10-Apr-05 18:02
memberrbeenlio10-Apr-05 18:02 
GeneralRe: renaming files. help! Pin
rbeenlio10-Apr-05 20:22
memberrbeenlio10-Apr-05 20:22 
GeneralRe: renaming files. help! Pin
Om Prakash P31-May-05 5:02
memberOm Prakash P31-May-05 5:02 
QuestionHow to refresh page when item selected in combo box Pin
crazy_guy24-Mar-05 7:49
susscrazy_guy24-Mar-05 7:49 
QuestionHow to download file from database? Pin
Gundars18-Feb-05 3:51
sussGundars18-Feb-05 3:51 
AnswerRe: How to download file from database? Pin
Om Prakash P18-Feb-05 8:05
memberOm Prakash P18-Feb-05 8:05 
GeneralRe: How to download file from database? Pin
Gundars19-Feb-05 9:31
sussGundars19-Feb-05 9:31 
GeneralRe: How to download file from database? Pin
Ron Harding3-May-05 14:07
memberRon Harding3-May-05 14:07 
GeneralRe: How to download file from database? Pin
yunan70731-May-05 18:24
memberyunan70731-May-05 18:24 
GeneralDisplay image when a click occur Pin
Anonymous20-Oct-04 21:15
sussAnonymous20-Oct-04 21:15 
GeneralRe: Display image when a click occur Pin
Om Prakash P27-Oct-04 0:51
memberOm Prakash P27-Oct-04 0:51 
Generallulu Pin
lulu505022-Sep-04 12:48
memberlulu505022-Sep-04 12:48 
QuestionWhat about multi part Tiff's Pin
mikedibblm@odjfs.state.oh.us1-Jun-04 5:41
membermikedibblm@odjfs.state.oh.us1-Jun-04 5:41 
Generaljust a note... Pin
l a u r e n31-May-04 9:09
memberl a u r e n31-May-04 9:09 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150427.4 | Last Updated 31 May 2004
Article Copyright 2004 by Om Prakash Pant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid