Results 1 to 6 of 6

Thread: Retriving images from a database

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    7

    Retriving images from a database

    Hey all I need some help with displaying some images that are in a database. Currently when I try to display them they come out in binary form and I am stumped on how to correct it.

    Server info:

    WWW Server:
    Windows Server 2003
    ASP .Net
    Pages are .aspx written in VBScript

    Database Info:
    MS SQL Server 2000

    Problem code:

    <%
    Dim rs
    rs = Server.CreateObject("adodb.recordset")
    rs.Open("Select * From avatar", Application("Conn"))
    If not rs.eof then
    Do Until rs.eof
    Response.ContentType = "image/gif"
    Response.binarywrite(rs.fields("a_avatar").value)
    rs.movenext
    Loop
    rs.close
    End If
    %>

    As stated above the result is display of the three images in the database in binary form.
    http://test.adiktclan.com/test.aspx

    Any help is greatly appreciated.

    Thanks,

    John K
    MCSA, A+

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Retriving images from a database

    I was going to say read Beacons tutorial.. but since you are using Asp, don't you have to store the pic in a temp folder and delete it after say twenty mins?
    You'd still have to point to a file for the users browser to determine that it's an image.

    I may be wrong tho

    Have you tried searching online via google or something for this?

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    7

    Re: Retriving images from a database

    Yes, but i get the information on how to do in ASP which i can get working but don't know what i am missing to get working in ASP .Net. As you can see from the link on the orignal post i get the binary from the database but doesn't get on converted back to "image". If i put inside a <img> tag all i get is small image boxes with nothing in them.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Retriving images from a database

    Check out this link:

    http://www.codeproject.com/aspnet/in...isse_part6.asp

    (Here is the search I did, and that link was the first one listed, but there may be others that could help as well, so here is my Goggle search: http://www.google.com/search?hl=en&q...=Google+Search )

  5. #5
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: Retriving images from a database

    Quote Originally Posted by mcp87
    Yes, but i get the information on how to do in ASP which i can get working but don't know what i am missing to get working in ASP .Net. As you can see from the link on the orignal post i get the binary from the database but doesn't get on converted back to "image". If i put inside a <img> tag all i get is small image boxes with nothing in them.
    Yes you will have to use Img tag. This is how I do it.

    First Put the code in a separate asp file, lets say GetImage.asp
    Now modify it so it can accept a parameter so we can show fetch one image at a time. e.g

    VB Code:
    1. <%
    2. Dim rs
    3. dim ID
    4. ID = Request.QueryString("ID")
    5. rs = Server.CreateObject("adodb.recordset")
    6. rs.Open("Select * From avatar Where AvatarID=" & ID, Application("Conn"))
    7. if not rs.eof then
    8. Response.ContentType = "image/gif"
    9.  
    10. Response.binarywrite(rs.fields("a_avatar").value)
    11. end if
    12. rs.close
    13.  
    14. %>

    Now in a spearate page where you want to display the Image

    Just loop through the record and pass the ID.

    e.g

    Do while not rs.eof
    response.write "<img src='GetImage.asp?ID=" & rs("ID") & "'>"
    rs.movenext
    loop

    That should work.

    BTW: I have posted some example and links for this sort of thing on this and database forum. Search my post you will find them, not that most of the solution I posted are for ASP and not asp.net.

    Ecniv, you can generate dynamic image like this without the need of pointing to any file.
    Last edited by Danial; Jan 26th, 2005 at 08:33 AM.
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    7

    Post Re: Retriving images from a database

    Ok i have it setup now like you suggested, the binary on the screen is gone but all i have now is image boxes with red x's. here ill post both of my files maybe i am over looking something simple and dumb.

    test.aspx

    VB Code:
    1. <%@ Page aspCompat="True" %>
    HTML Code:
    <html>
    <body>
    <center>
    <FORM METHOD="POST" ENCTYPE="multipart/form-data" ACTION="/test.aspx?content=processing"> 
    File 1:<INPUT TYPE=FILE NAME="FILE1" size="20"> 	 
    <INPUT TYPE=SUBMIT VALUE="Upload!"> 
    </FORM
    </center>
    VB Code:
    1. <%
    2. Dim rs
    3. Dim Upload
    4. Dim File
    5.    
    6. If Request.QueryString("content") = "processing" Then
    7.     Upload = Server.CreateObject("Persits.Upload.1") '
    8.     Upload.Save
    9.     rs = Server.CreateObject("adodb.recordset")
    10.              rs.CursorType = 1
    11.     Rs.LockType = 2
    12.     rs.Open("avatar", Application("Conn"))
    13.     rs.AddNew
    14.     File = Upload.Files("FILE1")
    15.     If Not File Is Nothing Then
    16.     rs("a_avatar").Value = File.Binary
    17.     rs.Update
    18.     End If
    19.     Response.Write("File added to database")
    20.     Rs.close
    21. End If
    22.  
    23. rs = Server.CreateObject("adodb.recordset")
    24. rs.Open("avatar", Application("Conn"))
    25. Do Until Rs.Eof
    26.      Response.Write("<img src='test2.aspx?id=" & rs.fields("a_id").value & "'>")
    27.      Rs.MoveNext
    28. Loop
    29. Rs.Close
    30. %>
    HTML Code:
    </body>
    </html>
    test2.aspx
    VB Code:
    1. <%@ Page aspCompat="True" %>
    2.  
    3. <%
    4. dim rs
    5. rs = Server.CreateObject("adodb.recordset")
    6. rs.Open("Select * From avatar where a_id ='" & Request.QueryString("id") & "'", Application("Conn"))
    7. Response.ContentType = "image/jpeg"                
    8. Response.binarywrite(rs("a_avatar").value)         
    9. rs.movenext
    10. rs.close
    11. End If
    12. %>
    Last edited by mcp87; Jan 27th, 2005 at 03:18 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width