Forum Moderators: open

Message Too Old, No Replies

Displaying images from links in database (ASP)

I need to know how to display images from a database link in ASP

         

Meyer9

7:38 am on Sep 15, 2003 (gmt 0)

10+ Year Member



I've searched all over the internet to no avail on this question.
I'm creating a database driven photo gallery
I don't want to embed my photos into the database, I just want the URL to be in the columns

I created a new database with a table named (PHOTO)
in the table the categories listed are (id) and (source).

ID's range from 0-41, and an example of what's in my source column is [www10.brinkster.com...]
Ive even tried not using the full URL to no success.

I'm creating a photo gallery that will host 42 pictures and a single page. I need to know how to get an image to appear from text written in a database . I have a page called photographs.asp that has an images named

<img src="photos.asp?id=0">
all the way down to
<img src="photos.asp?id=41">

I need help on getting the images from photos.asp to photographs.asp

Here's the source for photos.asp:

-----------------------------------------------------------

<% OPTION EXPLICIT %>
<%
'On Error Resume Next
Dim sql
Dim rs
Dim oconn
Dim sConnString
Dim strID

'Get querystring parameter
strID = Request("id")
If strID = "" Then strID = 0

'oConn.Open(sConnString)

'Instantiate Objects
Set oconn = Server.CreateObject("ADODB.Connection")
sConnString = "DRIVER={Microsoft Access Driver (*.mdb)};" & _
"DBQ=" & Server.MapPath("\jfhs\db\web.mdb") & ";"
Set rs = Server.CreateObject("ADODB.Recordset")

'Open connection
oConn.Open(sConnString)
'Application("aspdb_ConnectionString")

'Get the specific image based on the ID passed in a querystring
sql = "SELECT f.source " & _
" FROM photo f " & _
" WHERE f.id = " & strID
rs.Open sql, oconn

'If no record found, end.
if rs.eof then
rs.Close
Response.End
end if

'Display the contents of the record as an image
Response.ContentType = "image/jpeg"

'----------!Don't know what to use here!----------------

'Response.BinaryWrite(rs("source"))

'Response.Write ("<img src=""" & rs("source") & """ >")

'Response.Write(source)

'---------------------------END-----------------------------

'Clean up
rs.Close
oconn.Close
set rs = Nothing
set oconn = Nothing
%>

Thanks so much,

Brandon Meyer

aspdaddy

10:13 am on Sep 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




'----------!Don't know what to use here!----------------

'Response.BinaryWrite(rs("source"))

'Response.Write ("<img src=""" & rs("source") & """ >")

'Response.Write(source)

'---------------------------END-----------------------------

Just use:
<img src="<%=rs("source")%>">


Response.ContentType = "image/jpeg"

Not needed

txbakers

2:46 pm on Sep 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right - just think of your image name as another piece of text data coming from the database. I do this all the time, and I include the Alt text, the href, width, height, and other information in the file.

Everything about the image code is dynamic:

<a href="<%=rs("href")%>"><img src="<%=rs("source")%>" alt="<%=rs("alt")%>" width="<%=rs("width")%>" height="<%=rs("height")%>" border="0"> </a>

Meyer9

5:57 pm on Sep 15, 2003 (gmt 0)

10+ Year Member



I changed a couple of things mentioned above, but it still won't work.

Here is the script for test.asp

It properly displays the image url when I open
"test.asp?id=0"

My problem is, is that when I try to use the image in a seperate page, <img src="test.asp?id=0">, it fails to display the image. I'm trying to get the image to appear on seperate pages, not test.asp. It only serves as an interface.

Thanks again

<%

Dim sql
Dim rs
Dim oconn
Dim sConnString
Dim strID

'Get querystring parameter
strID = Request("id")
If strID = "" Then strID = 0

'oConn.Open(sConnString)

'Instantiate Objects
Set oconn = Server.CreateObject("ADODB.Connection")
sConnString = "DRIVER={Microsoft Access Driver (*.mdb)};" & _
"DBQ=" & Server.MapPath("\jfhs\db\web.mdb") & ";"
Set rs = Server.CreateObject("ADODB.Recordset")

'Open connection
oConn.Open(sConnString)
'Application("aspdb_ConnectionString")

'Get the specific image based on the ID passed in a querystring
sql = "SELECT f.source " & _
" FROM photo f " & _
" WHERE f.id = " & strID
rs.Open sql, oconn

'If no record found, end.
if rs.eof then
rs.Close
Response.End
end if

'Display the contents of the record as an image

'Response.BinaryWrite(rs("source"))

'Response.Write ("<img src=""" & rs("source") & """ >")

Response.Write rs("source")

'Clean up

rs.Close
oconn.Close
set rs = nothing
set oconn = Nothing
%>

txbakers

6:13 pm on Sep 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You don't response.write "source". All that does is give you the path to the image.

You response.write the entire HTML string, or just include the response.write of the path within the HTML string.

(see my post above for the code)

f00sion

6:21 pm on Sep 15, 2003 (gmt 0)

10+ Year Member



you can not do a binary.write with just a uri to an image.. you should instead save the local path to the file in the database and you will be able to do something like this:

with response
.Expires=0
.Expires=-1
.ExpiresAbsolute = #1/1/1980#
.AddHeader "Pragma", "no-cache"
.ContentType = "image/jpeg"
end with

Dim oStream
set oStream = server.createobject("ADODB.Stream")
oStream.Type = 1
oStream.Open
oStream.LoadFromFile rs("source")
response.binarywrite oStream.read
set oStream=nothing
response.End

this way you can use the .asp page in the <img> tag and it will embed it in the page.

f00sion

6:26 pm on Sep 15, 2003 (gmt 0)

10+ Year Member



on further thought, my solution is overkill... why not just pull each row on the first page and loop through them. then you will simply have to do <img src="<%=rs("source")%>"> and be done with it.

Meyer9

6:58 pm on Sep 15, 2003 (gmt 0)

10+ Year Member



I think we're reading two different pages. I didn't convey my problem properly.

I'm going to have two pages and a database.

Photograph.asp - will display an image based on ID #
dm.asp - will open the database and extract the image URL
web.mdb - contains records of ID# and Image URL

So basically:

Photograph.asp requests the image url from db.asp
<img src="db.asp?id=0">

db.asp opens the database and extracts record 0 from the database.

Request = Photograph.asp -> db.asp -> web.db

When I open db.asp, the image url shows up, I can also make the image appear using response.write

I've tried the line of code that txbakers and foosion wrote, but still not working. How do I get an image tag in photograph.asp to recognize the url in db.asp?

Thanks again,

Brandon

macrost

9:45 pm on Sep 15, 2003 (gmt 0)

10+ Year Member



Meyer9,
Look in your code to find this: strID = Request("id")
I see that you are using variables on a link, so you should use: strID = Request.QueryString("id")

'Get querystring parameter
strID = Request.QueryString("id")
response.write(strID)
If strID = "" Then strID = 0

Now for the displaying of the images:


'Get the specific image based on the ID passed in a querystring
sql = "SELECT f.source FROM photo f WHERE f.id =" & strID
rs.Open sql, oconn
While Not rs.EOF
Response.Write("<img src='" & rs("photourl") & "'>")
rs.MoveNext
Wend
rs.Close
Response.End
end if

This should work.

Mac

garann

10:30 pm on Sep 15, 2003 (gmt 0)

10+ Year Member



I've tried the line of code that txbakers and foosion wrote, but still not working. How do I get an image tag in photograph.asp to recognize the url in db.asp?

Excuse my butting in, but I can't see this explicitly mentioned anywhere: The majority of these solutions look like code for photograph.asp, not db.asp/test.asp/photo.asp.

Unless you're doing watermarking or image resizing, there's no obvious reason to use a seperate page to retrieve the URL from the database. You can't use text in the src attribute of an image, which is what you appear to be producing in db.asp. That's probably why using db.asp?id=0 as the src attribute isn't working for you.

hth,
g.

too much information

10:50 pm on Sep 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure there's a reason to do this. Now when someone looks at your source code to see where your images come from, the follow to the asp page that loads the image and because the page is not being called from within another page you can switch the image to something like a transparent gif, or a pic of your dog sticking his tounge out.

Here's what you're looking for. Don't call the image page up using the <img src=""> tag, use an <iframe src=""></iframe> instead. you will have to specify the height and width of the iframe to get it to fit every time, but I think that is what you are looking for.

Good luck getting the cookies set up to verify that the images are loading from the page and not from a direct visit. If you are trying to do this for multiple images per page it can be tricky.

Good luck!

aspdaddy

9:02 am on Sep 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Another way is to use a redirect,

<img src=showimage.asp?a=10>

strImage="/images/a" & request("a") & ".jpg"
response.redirect strImage