SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Problem Displaying Images from Database

    Hi,

    I am building a website where users can place advertisements with images. I have decided to place the images in an Access database to ensure the right images are matched the correct advertisements. My problem is that I can retrieve all of the data from the table which holds the photos (eg, filename, file size, content type) except for the actual photo itself. When I try to download the photo or press the hyperlink to display the photo all I get is the square with the red cross. I do not get any error messages.

    Does anyone know why this is happening. My code is below:

    Advertisement page

    Code:
    <%
    Response.Buffer = True
    	
    ' Connection String
    Dim connStr
    	connStr = ""Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\wwwsites\website.com\odbc\database.mdb;"%>
    ' Recordset Object
    Dim rs
    Set rs = Server.CreateObject("ADODB.Recordset")
    		
    ' opening connection
    rs.Open "select [account_id],[file_name],[file_size],[content_ type],[photo] from photo order by [account_id] desc", connStr, 3, 4
    
    If Not rs.EOF Then
    Response.Write "<tr><td colspan=""7"" align=""center""><i>"
    Response.Write "No. of records : " & rs.RecordCount
    Response.Write ", Table : photo</i><br>"
    Response.Write "</td></tr>"
    While Not rs.EOF
    Response.Write "<tr><td>"
    Response.Write rs("account_id") & "</td><td>"
    Response.Write "<a href=""show_picture.asp?account_id=" & rs("account_id") & """>"
    Response.Write "<img src=""show_picture.asp?account_id=" & rs("account_id") & """>"
    Response.Write rs("file_name") & "</a></td><td>"
    Response.Write rs("file_size") & "</td><td>"
    Response.Write rs("content_type") & "</td><td>"
    Response.Write "</td></tr>"
    rs.MoveNext
    Wend
    Else
    Response.Write "No Record Found"
    End If
    		
    rs.Close
    Set rs = Nothing
    %>
    </table>
    </body>
    </html>
    show_picture.asp page that retrieves the image

    Code:
    <%@LANGUAGE="VBSCRIPT"%>
    <%
    ' Retrieves binary files from the database
    	
    Response.Buffer = True
    
    'Declare Variables..
    Dim Image
    Dim rs
    Dim connStr
    Dim ID,str
    Dim PicSize	
    
    
    ' ID of the file to retrieve
    ID = Request.QueryString("account_id")
    If ID = "" Then ID = 0
             
    
    If Len(ID) < 1 Then
    		response.write("Image not here")
    	End If
    	
    If ID <> "" then	
    'Instantiate Objects
    Set connStr = Server.CreateObject("ADODB.Connection")
    Set rs = Server.CreateObject("ADODB.Recordset")	
    	
    ' Connection String
    	connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\wwwsites\website.com\odbc\database.mdb;"
    
    'Get the specific image based on the ID passed in a querystring
        str = "SELECT photo, content_type FROM photo where account_id =" & ID
    	rs.Open str, connStr, adOpenDynamic
    	if not (rs.eof and rs.bof) then
    	PicSize = rs("Photo").ActualSize
    	response.write PicSize 
    	Image = rs("photo").GetChunk(PicSize)
    	Response.ContentType = "image/pjpeg" 
        Response.BinaryWrite Image 
    	else
        Response.Write "ID '" & ID & "' not found in DB."
        end if
    End If	
    
    'destroy the variables.
    rs.Close  
    set rs = Nothing
    set connStr = Nothing  
    %>
    Thanks in advance

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well this would definitely mess it up:

    response.write PicSize

  3. #3
    SitePoint Addict danfran's Avatar
    Join Date
    Jan 2005
    Location
    New York City
    Posts
    244
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Agreed, Jim..

    Here's a snippet from an old project of mine..
    Code:
    	' output
    	Response.ContentType = "image/jpeg"
    	Response.Clear
    	Response.BinaryWrite Obj.GetImageAsJpeg
    See if you have any luck with clearing the buffer before yer BWrite..

    Dan

  4. #4
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your quick responses.

    I have changed my code but I still have the same problem. Do you have any other ideas?

    Here is the new code:


    <%@LANGUAGE="VBSCRIPT"%>
    <%
    ' Retrieves binary files from the database

    Response.Buffer = True

    'Declare Variables..
    Dim Image
    Dim rs
    Dim connStr
    Dim ID,str
    Dim PicSize


    ' ID of the file to retrieve
    ID = Request.QueryString("account_id")
    response.write request.querystring
    If ID = "" Then ID = 0


    If Len(ID) < 1 Then
    response.write("Image not here")
    End If

    If ID <> "" then
    'Instantiate Objects
    Set connStr = Server.CreateObject("ADODB.Connection")
    Set rs = Server.CreateObject("ADODB.Recordset")

    ' Connection String
    connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\wwwsites\website.com\odbc\database.mdb;"

    'Get the specific image based on the ID passed in a querystring
    str = "SELECT photo, content_type FROM photo where account_id =" & ID
    rs.Open str, connStr, adOpenDynamic
    if not (rs.eof and rs.bof) then
    PicSize = rs("photo").ActualSize
    Image = rs("photo").GetChunk(PicSize)
    Response.ContentType = "image/pjpeg"
    Response.Clear
    Response.BinaryWrite Image.GetImageAsJpeg
    else
    Response.Write "ID '" & ImageID & "' not found in DB."
    end if
    End If

    'destroy the variables.
    rs.Close
    set rs = Nothing
    set connStr = Nothing
    %>

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See what happens if you:

    1. Get rid of Response.ContentType = "image/pjpeg"
    2. Hit the page directly, ie type in url
    http://myserver/mypath/myfile.asp?account_id=someid

    Maybe you'll get a useful error message. If you see a pile of garbage characters, that's good, and the problem is elsewhere.

  6. #6
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for getting back to me.

    When I take out the Response.ContentType, I get the following error:

    Microsoft VBScript runtime error '800a01a8'
    Object required: '?????'
    /show_picture.asp, line 40

    Line 40 is: Response.BinaryWrite Image.GetImageAsJpeg

    Does this error message give you any ideas?

    Thanks for your help.

  7. #7
    SitePoint Addict danfran's Avatar
    Join Date
    Jan 2005
    Location
    New York City
    Posts
    244
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, hey, hey...
    Response.BinaryWrite Image.GetImageAsJpeg
    That part is proprietary to my own image component.. You should stick to your original plan.. Sorry if I confused you with this.. :-)

    Dan

  8. #8
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Thanks for letting me know ... that's my fault - I misread your answer.

    I have taken away the GetImageAsJpeg and the good news is that I can display my images from my database.


    Thank you Dan and Jim for helping me out - I really appreciate it. I have been working on trying to display the picture for more than 1 week - I am new and self-taught to ASP and some things are easier said than done for me at the moment.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •