Jagar
asked on
Displaying a Picture
Hello I have an OLE Object field in an Access database. This field has a JPEG picture saved in it. what I need to do is display this image on my website using Active Server Pages. Can someone give me code basically this is what I looking for a function that takes a parameter. This parameter will be the field that the picture is located in. The function will return the image exactly the way it should appear on the page, ie
<img src=<%=GetPicture(pic1)%> border=0>
That's what I want to be able to write and have the picture displayed.
<img src=<%=GetPicture(pic1)%> border=0>
That's what I want to be able to write and have the picture displayed.
ASKER
That's not what I asked for I already have database connection open with the field and everything. What I need is a function that will use the already open database connection.
I am sorry, but I still think this is what you want. You have to define the image as an external file. The "src" in the image tag ALWAYS points to a URL.
I am afraid that it is not possible to write the binary code for the image in am HTML file. If it is, then, it's not really gonna be easy.
By the way, the database connection you have is not completely useless, because you have have to pass an ID to ShowImage.asp in the querystring. This ID should come from the recordset you have.
I am afraid that it is not possible to write the binary code for the image in am HTML file. If it is, then, it's not really gonna be easy.
By the way, the database connection you have is not completely useless, because you have have to pass an ID to ShowImage.asp in the querystring. This ID should come from the recordset you have.
Response.ContentType = "image/GIF"
Response.Binarywrite RSImage("imagefield")
This is from sybe... this is what you need to add to your "already open" database to get the file to display... Sybe, your answer is correct...
Response.Binarywrite RSImage("imagefield")
This is from sybe... this is what you need to add to your "already open" database to get the file to display... Sybe, your answer is correct...
ASKER
OK SYBE I'll try that reenter you answer and I'll grade it after I check it out. Sorry about the rejection, I wanted to do it that way, but I guess it's not possible. Thank You.
ASKER
I'm having a problem I getting broken pictures. Here is what I have
Here is the <A> tag, actually three of them side by side
<img src="ShowImage.asp?id=1&Wh ich=1&Tabl e=1" Border=0>
<img src="ShowImage.asp?id=1&Wh ich=2&Tabl e=1" Border=0>
<img src="ShowImage.asp?id=1&Wh ich=3&Tabl e=1" Border=0>
Here is ShowImage.asp
<%
Response.ContentType = "image/GIF"
Set RS = Server.CreateObject("ADODB .Recordset ")
if Response.QueryString("Whic h") = 1 then
Which="Pic1"
elseif Response.QueryString("Whic h") = 2 then
Which="Pic1"
elseif Response.QueryString("Whic h") = 3 then
Which="Pic1"
end if
if Response.QueryString("Tabl e") = 1 then
Table="Directors"
elseif Response.QueryString("Tabl e") = 2 then
Table="AirParks"
elseif Response.QueryString("Tabl e") = 3 then
Table="Properties"
end if
strSQL = "SELECT " & Which & " FROM " & Table & " WHERE id=" & Request.Querystring("id")
RSImage.Open strSQL, "DSN=AirMLS;uid=" & Session("uid") & ";pwd=" & Session("pwd"), 1, 1
Response.Binarywrite RSImage("imagefield")
RSImage.Close %>
Any idea why.
Here is the <A> tag, actually three of them side by side
<img src="ShowImage.asp?id=1&Wh
<img src="ShowImage.asp?id=1&Wh
<img src="ShowImage.asp?id=1&Wh
Here is ShowImage.asp
<%
Response.ContentType = "image/GIF"
Set RS = Server.CreateObject("ADODB
if Response.QueryString("Whic
Which="Pic1"
elseif Response.QueryString("Whic
Which="Pic1"
elseif Response.QueryString("Whic
Which="Pic1"
end if
if Response.QueryString("Tabl
Table="Directors"
elseif Response.QueryString("Tabl
Table="AirParks"
elseif Response.QueryString("Tabl
Table="Properties"
end if
strSQL = "SELECT " & Which & " FROM " & Table & " WHERE id=" & Request.Querystring("id")
RSImage.Open strSQL, "DSN=AirMLS;uid=" & Session("uid") & ";pwd=" & Session("pwd"), 1, 1
Response.Binarywrite RSImage("imagefield")
RSImage.Close %>
Any idea why.
It all depends on the database now.
If you show me the which tables you have and which fields those tables have, and which field the image is in, then I can help you.
The code looks well, accept I cannot check if the strSQL is valid for the database you use.
If you show me the which tables you have and which fields those tables have, and which field the image is in, then I can help you.
The code looks well, accept I cannot check if the strSQL is valid for the database you use.
I guess you got it working now, because you don't have anymore questions. So it's time to get my points :)
ASKER
Actually I had to leave work early yesterday, so I never was able to get you response until today. I have not fixed it. Do you want me to E-Mail you the .MDB file or just tell you. If I tell its gonna be exactly what is written. I have three pictures fields Pic1, Pic2, Pic3. In three different tables, Directors, Properties, and Airparks.
I know in the above code it says Pic1, Pic1, Pic1, but I've changed that. Also all the fields and stuff are valid, because otherwise I would get and error when it tried to use that as a SQL string, but I am not getting and error, just broken pictures.
I won't reject your answer because Massey said that it was correct, but I can't award the points without it working correctly.
I know in the above code it says Pic1, Pic1, Pic1, but I've changed that. Also all the fields and stuff are valid, because otherwise I would get and error when it tried to use that as a SQL string, but I am not getting and error, just broken pictures.
I won't reject your answer because Massey said that it was correct, but I can't award the points without it working correctly.
I understand that.
There is a way to see the ASP error in the picture. You can do that by rightclicking on the (broken) image and choose "View Image". It then opens in your browser, it will still be broken, but now you can do "view source". There you get the error message.
Do that and show the error message here.
There is a way to see the ASP error in the picture. You can do that by rightclicking on the (broken) image and choose "View Image". It then opens in your browser, it will still be broken, but now you can do "view source". There you get the error message.
Do that and show the error message here.
ASKER
Thanks I never knew that, but you wanna know something you can't do that in IE. Here is waht the error says
Type mismatch: '[undefined]'
/airmls/ShowImage.asp, line 21</font>
Actually there was another error first, but I fixed that one. I don't know about this one though. Any Ideas
Type mismatch: '[undefined]'
/airmls/ShowImage.asp, line 21</font>
Actually there was another error first, but I fixed that one. I don't know about this one though. Any Ideas
Hmm, supposing that line 21 points to this line:
"Response.Binarywrite RSImage("imagefield") "
Then you should replace "imagefield" with the fieldname in the database which contains the image.
In you sql statement you do "SELECT which FROM..."
So I suppose that the field containing the image is called "which"
Line 21 should be then:
"Response.Binarywrite RSImage("which")"
===========
Another debuggng tip: If you want to see the error messages:
You can call the image directly in you browser by typing in the url of the image:
http://yourserver/yourpath/ShowImage.asp?id=1&Which=1&Table=1
In ShowImage.asp you can comment out (temporarily) the line that says:
Response.ContentType = "image/GIF"
(put an apostroph before it)
Then the browser will think it is a html output and any errors can be read. If there are no errors, you will see the binary code for the image. Then it is time to uncomment the "Response.ContentType" line. After you have done that, and you reload, you should see the image.
"Response.Binarywrite RSImage("imagefield") "
Then you should replace "imagefield" with the fieldname in the database which contains the image.
In you sql statement you do "SELECT which FROM..."
So I suppose that the field containing the image is called "which"
Line 21 should be then:
"Response.Binarywrite RSImage("which")"
===========
Another debuggng tip: If you want to see the error messages:
You can call the image directly in you browser by typing in the url of the image:
http://yourserver/yourpath/ShowImage.asp?id=1&Which=1&Table=1
In ShowImage.asp you can comment out (temporarily) the line that says:
Response.ContentType = "image/GIF"
(put an apostroph before it)
Then the browser will think it is a html output and any errors can be read. If there are no errors, you will see the binary code for the image. Then it is time to uncomment the "Response.ContentType" line. After you have done that, and you reload, you should see the image.
ASKER
I've made all the changes, but am still getting broken pictures. I ran the line
http://computername/airmls/ShowImage.asp?id=1&Which=1&Table=1
and I don't get any errors.
Now here is a question for you; I use MS Access 7.0 as my database and have an OLE Object field for the Pics. I double clicked the field and said to insert the object from file and then selected the file that I wanted to be displayed.
http://computername/airmls/ShowImage.asp?id=1&Which=1&Table=1
and I don't get any errors.
Now here is a question for you; I use MS Access 7.0 as my database and have an OLE Object field for the Pics. I double clicked the field and said to insert the object from file and then selected the file that I wanted to be displayed.
ASKER
I changed the way I added the image. No I used VB code to copy the file to the database.
I'm really stumped here got any ideas.
Here is the VB code that I used
Public Function SaveFileToDB(Source As String, BynaryField As Field) As Integer
' *** Will save a file to a Binary field
Dim nNumBlocks As Integer
Dim nFile As Integer
Dim nI As Integer
Dim nFileLen As Long
Dim nLeftOver As Long
Dim sFileData As String
Dim RetVal As Variant
Dim nBlockSize As Long
On Error GoTo Error_SaveFileToDB
nBlockSize = 32000 ' Set size of chunk.
' *** Open the source file.
nFile = FreeFile
Open Source For Binary Access Read As nFile
' *** Get the length of the file.
nFileLen = LOF(nFile)
If nFileLen = 0 Then
SaveFileToDB = 0
Exit Function
End If
' *** Calculate the number of blocks to read and nLeftOver bytes.
nNumBlocks = nFileLen \ nBlockSize
nLeftOver = nFileLen Mod nBlockSize
' *** Read the nLeftOver data, writing it to the table.
sFileData = String$(nLeftOver, 32)
Get nFile, , sFileData
BynaryField.AppendChunk (sFileData)
' *** Read the remaining blocks of data, writing them to the table.
sFileData = String$(nBlockSize, Chr$(32))
For nI = 1 To nNumBlocks
Get nFile, , sFileData
BynaryField.AppendChunk (sFileData)
Next
Close #nFile
SaveFileToDB = nFileLen
Exit Function
Error_SaveFileToDB:
SaveFileToDB = -Err
Exit Function
end function
I'm really stumped here got any ideas.
Here is the VB code that I used
Public Function SaveFileToDB(Source As String, BynaryField As Field) As Integer
' *** Will save a file to a Binary field
Dim nNumBlocks As Integer
Dim nFile As Integer
Dim nI As Integer
Dim nFileLen As Long
Dim nLeftOver As Long
Dim sFileData As String
Dim RetVal As Variant
Dim nBlockSize As Long
On Error GoTo Error_SaveFileToDB
nBlockSize = 32000 ' Set size of chunk.
' *** Open the source file.
nFile = FreeFile
Open Source For Binary Access Read As nFile
' *** Get the length of the file.
nFileLen = LOF(nFile)
If nFileLen = 0 Then
SaveFileToDB = 0
Exit Function
End If
' *** Calculate the number of blocks to read and nLeftOver bytes.
nNumBlocks = nFileLen \ nBlockSize
nLeftOver = nFileLen Mod nBlockSize
' *** Read the nLeftOver data, writing it to the table.
sFileData = String$(nLeftOver, 32)
Get nFile, , sFileData
BynaryField.AppendChunk (sFileData)
' *** Read the remaining blocks of data, writing them to the table.
sFileData = String$(nBlockSize, Chr$(32))
For nI = 1 To nNumBlocks
Get nFile, , sFileData
BynaryField.AppendChunk (sFileData)
Next
Close #nFile
SaveFileToDB = nFileLen
Exit Function
Error_SaveFileToDB:
SaveFileToDB = -Err
Exit Function
end function
Yes, I know, putting a picture as an OLE object in an OLE field, does not work.
I use an upload module to put images in a database. SA-FileUp from SoftArtisans (www.softartisans.com), there is a free trial version available.
At first I used also a VBA-script to get an image into a database field. I have to find it, but I think it used the GetChunk method.
I use an upload module to put images in a database. SA-FileUp from SoftArtisans (www.softartisans.com), there is a free trial version available.
At first I used also a VBA-script to get an image into a database field. I have to find it, but I think it used the GetChunk method.
That's what the above function uses actually AppendChunk to add and GetChunk to read from the database. I need to be able to this programactically.
ASKER
I done everything you said and still don't get pictures displayed.
The problem is I think with the contents of the database. Have you tried using the upload module from www.softartisans.com. ? That will put the image in the database in the right format.
I know I had some VBA code to put the image in the database, and that code worked, but I am afraid that I can't find it anymore.
I know I had some VBA code to put the image in the database, and that code worked, but I am afraid that I can't find it anymore.
ASKER
I have a routine that adds the picture to the database and displays it in Visual Basic. It does this in JPEG format and these work correctly.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Why does not the following code work. It seems to do everything you said above
<%
Response.ContentType = "image/JPEG"
Set RS = Server.CreateObject("ADODB .Recordset ")
if Response.QueryString("Whic h") = 1 then
Which="Pic1"
elseif Response.QueryString("Whic h") = 2 then
Which="Pic1"
elseif Response.QueryString("Whic h") = 3 then
Which="Pic1"
end if
if Response.QueryString("Tabl e") = 1 then
Table="Directors"
elseif Response.QueryString("Tabl e") = 2 then
Table="AirParks"
elseif Response.QueryString("Tabl e") = 3 then
Table="Properties"
end if
strSQL = "SELECT " & Which & " FROM " & Table & " WHERE id=" & Request.Querystring("id")
RSImage.Open strSQL, "DSN=AirMLS;uid=" & Session("uid") & ";pwd=" & Session("pwd"), 1, 1
Response.Binarywrite RSImage("imagefield")
RSImage.Close %>
<%
Response.ContentType = "image/JPEG"
Set RS = Server.CreateObject("ADODB
if Response.QueryString("Whic
Which="Pic1"
elseif Response.QueryString("Whic
Which="Pic1"
elseif Response.QueryString("Whic
Which="Pic1"
end if
if Response.QueryString("Tabl
Table="Directors"
elseif Response.QueryString("Tabl
Table="AirParks"
elseif Response.QueryString("Tabl
Table="Properties"
end if
strSQL = "SELECT " & Which & " FROM " & Table & " WHERE id=" & Request.Querystring("id")
RSImage.Open strSQL, "DSN=AirMLS;uid=" & Session("uid") & ";pwd=" & Session("pwd"), 1, 1
Response.Binarywrite RSImage("imagefield")
RSImage.Close %>
1) You didn't clear the HTTP first:
Response.Expires = 0
Response.Buffer = TRUE
Response.Clear
2) If you are using MS Access, it adds up more bits to the beginning of the OLE field, so the image is not raw data. You'd need to use SQL Server or another database that only saves the image data itself in the image field.
Response.Expires = 0
Response.Buffer = TRUE
Response.Clear
2) If you are using MS Access, it adds up more bits to the beginning of the OLE field, so the image is not raw data. You'd need to use SQL Server or another database that only saves the image data itself in the image field.
ASKER
If number two is right then how come I can use a similar function from within Visual Basic and load and save the pictures.
Visual Basic is database oriented, and it knows you are dealing with a OLE field. With Internet Browsers the story is a bit different. What you are doing with your code is translating the OLE field into a data stream of bits, that is the reason for the page named SHOWIMG.ASP. When you call <IMG SRC="SHOWIMG.ASP"> it has no idea whatsoever you are dealing with a database or a OLE object. It is expecting a plain picture.
ASKER
I give you the points, but I can't get this to work because of the configuration that I'm forced to used. Thank You for all your help
code for ShowImage.asp:
<%
Set Conn = Server.CreateObject("ADODB
Conn.Open "DSN=YourDSN"
Response.ContentType = "image/GIF"
Set RSImage = Server.CreateObject("ADODB
strSQL = "SELECT imagefield FROM table WHERE id=" & Request.Querystring("id")
RSImage.Open strSQL, Conn, 1, 1
Response.Binarywrite RSImage("imagefield")
RSImage.Close
%>
2.
Call it with:
<img src="ShowImage.asp?id=2" border=0>