Link to home
Start Free TrialLog in
Avatar of pohkong
pohkong

asked on

<IMG SRC="GetImage.asp"> ?

i try to get image from database with a  html like below:
<IMG SRC="GetImage.asp">

The GetImage.asp script may look like this:

<%
Set db = Server.CreateObject("ADODB.Connection")
db.Open "data"
Set rs =db.Execute("SELECT BigBlob FROM Blobs where id = 'ABC' )
Response.ContentType = "image/jpeg" '(or "image/gif")
Response.BinaryWrite rs("BigBlob")
%>

the question is i can't get the image in the html page. why? is that any wrong my method or coding?

can we display *.ico image on a button?
Avatar of xabi
xabi

I think you have a bad query, cause you forgot to close the double quotes of the query:

Try this query:

Set rs =db.Execute("SELECT BigBlob FROM Blobs where id = 'ABC'")

If don't work change your code into mine, this way:

<%
response.Expires = 0
response.Buffer  = True
response.Clear
response.contentType = "image/gif"

..
..
Read database
..
..
response.BinaryWrite rs("BigBlob")
rs.Close
Set rs = Nothing
response.End
%>


xabi
Avatar of pohkong

ASKER

Dear xabi,

i have try your way, but still the same.
hmmm this method works for me. Don't you get any error?

Try this:

comment the contentType line and maketry to open this page not as an Image but as an html page. So you can see the errors.
To do this just type in you browser:

http://www.yoursite.com/Getimage.asp

This way will see what's happening.

xabi
Avatar of pohkong

ASKER

dear xabi,

i have try your way, i got the following error message.

ADODB.Fields error '800a0cc1'

ADO could not find the object in the collection corresponding to the name or ordinal reference requested by the application.

/Project1/uploadscript4.asp, line 31

i am using the ms access to store the image in database as an ole object. do i need to do any setting on it.
Why don't you try your query in a query analizer or in your access? I think your query doesn't return you any row, so you don't have access to the field ("BigBlob"). It the query is right and you have the data and still don't work try this:

response.BinaryWrite rs.fields("BigBlob")

You can also continue debugging your script this way:

1.- continue with the content type line commented.
2.- add this t your code:

if rs.eof then
  response.write("No data")
else
  response.write("I have some data")
end if

instead of

response.BinaryWrite rs("BigBlob")

xabi
are you keeping the whole image tag in the database or just the name?

if just the name of the image you can use

<img src="images/<%=BigBlog%>">
Avatar of pohkong

ASKER

dear xabi,
i have try sql statement in access, i got data from the sql statement. and i also run the script and i got the out "I have some data"
but the problem is i still can't get the image in the html page.
Avatar of pohkong

ASKER

dear crit,
i am using the ms access to store the image in database as an ole object. i think it is the whole image tag, isn't it? do i need to do any setting in order to get the image from the database?
ASKER CERTIFIED SOLUTION
Avatar of clockwatcher
clockwatcher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
dear clockwatcher,

do you know that the oleheadersize for jpeg or gif.
dear clockwatcher,

do you know that the oleheadersize for jpeg or gif.
dear clockwatcher,

do you know that the oleheadersize for jpeg or gif.
Avatar of pohkong

ASKER

dear clockwatcher,

do you know that the ole header size for jpeg or gif.
The actual object and raw data associated with it that gets stored in your Access database depends upon whatever object server you have registered to handle the particular file type you're inserting.  In my case (on the computer I'm currently working with), Corel Photopaint is registered to handle GIFs and JPGs, so when I embed a GIF or JPG into Access (using insert object), Corel takes over and stores it however it wants to see the data (a CPT file with additional OLE header information).  Basically, what I'm saying is that the raw data associated with your object isn't necessarily going to still be a gif or a jpg-- it's going to be decided by whatever server object controls the OLE document.

The easiest way to store images within an Access database is to store the raw data yourself, rather than store the image as an OLE object (with Insert Object).  For example, suppose you have the following fields:

TblPictures
  PictureID  Autonumber
  Type       Text(10)
  Picture    OLE

You can insert the raw data into your OLE field with the AppendChuck method.  So for example, the following Access function will store an image as a BLOB datatype within the Picture field.

Function addPicture(strFullPath)

    Dim rs As Recordset
    Dim fileBytes() As Byte
    Dim filenum As Integer
   
    ReDim fileBytes(FileLen(strFullPath))
   
    filenum = FreeFile()
   
    Open strFullPath For Binary As filenum
    Get filenum, , fileBytes
    Close filenum
   
    Set rs = CurrentDb.OpenRecordset("tblPictures", dbOpenTable, dbAppendOnly, dbOptimistic)
    rs.AddNew
    rs("type") = IIf(Right(strFullPath, 3) = "jpg", "jpeg", Right(strFullPath, 3))
    rs("picture") = fileBytes
    rs.Update
    rs.Close
   
    Set rs = Nothing
   
End Function


You lose the OLE aspect of the object-- you can't double-click it and start up the server application.  But, you've now stored the raw data into your field, so you can simply binary write it on your ASP page.  If you actually open up tblPictures and look at the data, you'll see that the description of picture says Large Binary Object not Corel Photopaint Image (or whatever you've got registered).

Anyway, I posted a sample that pulls the three images on the page from an Access database-- two are raw images (one gif, one jpg) the other is an OLE BMP image.  You can check it out at:

  http://experts-exchange.yahright.com/accesspicture/

A zip file containing the database, the asp page, and the html file is at:

  http://experts-exchange.yahright.com/accesspicture/accesspicture.zip

The following is the source for the getpicture.asp page which actually pulls the image from the database:

<%

  Const OLEHEADERSIZE = 78
 
  strconn = "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=" & server.mappath(".") & "\picturedb.mdb"
  set rs = server.createobject("ADODB.Recordset")
  rs.open "select * from tblPictures where pictureid=" & request.querystring("id"), strconn
 
  if not rs.eof then
     
     select case rs("type")
        case "olebmp":  
 
            ctype = "image/bmp"
            nFieldSize = rs("picture").ActualSize
            oleHeader = rs("picture").GetChunk(OLEHEADERSIZE)
            imageBytes = rs("picture").GetChunk(nFieldSize - OLEHEADERSIZE)
       
       case else:  

            ctype = "image/" & rs("type")
            imageBytes = rs("picture")

     end select
  end if
 
  rs.close
  set rs = nothing

  response.contenttype = ctype
  Response.BinaryWrite imageBytes

%>