By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,760 Members | 1,644 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,760 IT Pros & Developers. It's quick & easy.

Display image from a database get me ascii codes

P: 9
All i wanna do is simple, I wanna display an image from an access database.

I try alot of things to no result. Like ascii code or nothing but a blank image with a red 'X'.

Here is the code :

<%
set conn=server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.jet.oledb.4.0"
conn.Open(server.MapPath("test1.mdb"))
set rs= conn.Execute("SELECT photo FROM photos where ID=1")
Response.ContentType="image/jpg"
Response.BinaryWrite(rs("photo"))
conn.Close
%>

I also tried a file named ShowPicture.asp and call it from another file like this:
<IMG SRC="ShowPicture.asp?PhotoId=1">

ShowPicture.asp
<%
'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.Provider="Microsoft.jet.oledb.4.0"
conn.Open(server.MapPath("test1.mdb"))

'Get the specific image based on the ID passed in a querystring
str = "SELECT photo FROM photos where ID =" & userID
rs.Open str, conn,3,3

if rs.eof then 'No records found
Response.End
else 'Display the contents
Response.ContentType = "image/jpg"
Response.BinaryWrite(rs("photo"))
end if

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

I also try this :

<body>
<%
set conn=server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.jet.oledb.4.0"
conn.Open(server.MapPath("test1.mdb"))
set rs= conn.Execute("SELECT photo FROM photos where ID=1")
Response.ContentType="image/jpg"
%>

<img src="<%Response.BinaryWrite(rs("photo"))%>">

<% conn.Close %>
</body>

All i got is tons ascii codes.

Can you help me on this ?

thanx
Chris
Oct 28 '06 #1
Share this Question
Share on Google+
2 Replies


P: 2
You are on the right path when referencing your image

e.g. <img src="file_name.asp?photoId=4">

Here is a copy of my file that works

<%@ Language=VBScript %>
<!--#include virtual="/_includes/connString.asp"-->
<%
Dim rs
Dim qry_getImage
Dim intPic_id

intPic_id = trim(Request.QueryString("id"))

' Clear out the existing HTTP header information
Response.Expires = 0
Response.Buffer = TRUE
Response.Clear

qry_getImage = "SELECT PHOTO_ALT_TEXT_EN, PHOTO_ALT_TEXT_FR, PHOTO_FILE "
qry_getImage = qry_getImage & "FROM CLASSIFIED_PHOTO "
qry_getImage = qry_getImage & "WHERE (PHOTO_ID = " & intPic_id & ") "

'Response.Write qry_getImage
' response.end

set rs = Server.CreateObject("ADODB.Recordset")
Set rs = conn.Execute(qry_getImage)

if not (rs.BOF and rs.EOF) then
' Change the HTTP header to reflect that an image is being passed.
Response.ContentType = "image/gif"
Response.BinaryWrite rs("PHOTO_FILE")
Response.Write rs("PHOTO_ALT_TEXT_EN")
Response.Write ("<br>")
Response.Write rs("PHOTO_ALT_TEXT_FR")
else
Response.Redirect "/_images/spacer.gif"
end if

Response.End
%>
Nov 16 '06 #2

P: 2
Actually, scratch the part where I write the caption text as well, this has to be done seperatly.
Nov 16 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.