Go Back   CodingForums.com > :: Server side development > ASP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-17-2004, 10:37 AM   PM User | #1
kate perry
New to the CF scene

 
Join Date: Aug 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
kate perry is an unknown quantity at this point
Displaying alternate image when image source not found

I am creating a dynamic ASP VBScript page, which gets data from an Access database.

In the database, there is a field which holds a URL to an image. I can get the image to display fine, but where there is no file that matches the URL (i.e. a missing image) I want to display an alternative, default image (e.g. one that says "Awaiting Image"). The field is never blank, but the image file may not exist for all records in the database.

I think what I am trying to get to is the following:
If file exists (using URL from database to get location of image file) then
display image using the url from the database as the image source
else
display default image - hardcoded in program
endif

I have had a go and come up with the attached but I get an error with my if statement.

<%
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists (server.mappath("<%=(Recordset1.Fields.Item("ImageLocation").Value)%>")) Then
<img src="<%=(Recordset1.Fields.Item("ImageLocation").Value)%>">
Else
<img src="images/awaitingimage.jpg" width="243" height="239">
End If
%>
kate perry is offline   Reply With Quote
Old 08-17-2004, 02:15 PM   PM User | #2
head8k
New Coder

 
Join Date: Jun 2002
Location: London & Oxford
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
head8k is an unknown quantity at this point
You have the right idea. You can simplify your code by setting the value pulled from your database to a variable as shown below. there was also a problem with your code in that you were mixing VBScript and HTML code without properly switching between the two. Try this:

<%
Dim objFSO, imagePath

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

imagePath = Recordset1.Fields.Item("ImageLocation").Value

If objFSO.FileExists (server.mappath(imagePath)) Then
Response.Write "<img src="""&imagePath&""" />"
Else
Response.Write "<img src=""images/awaitingimage.jpg"" />"
End If
%>

If this errors it is most likely going to be caused by the code not being able to map the path to the image correctly. Are you storing the full path in the database? I.e. c:\inetpub\wwwroot\whatever\images\img1.jpg or just a relative path?
__________________
As easy as 3.1415926535897932384626433832795028841
head8k is offline   Reply With Quote
Old 08-17-2004, 02:31 PM   PM User | #3
kate perry
New to the CF scene

 
Join Date: Aug 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
kate perry is an unknown quantity at this point
I think you are right about it being because of the path I am using.
When I run the following code:

<%
Dim imgName, imgPath
imgName = "\" & (Recordset1.Fields.Item("ImageLocation").Value)
imgPath = Server.MapPath(imgName)
response.write(imgName)
response.write "<br>"
response.write(imgPath)
response.end
%>

I get:
images\5003301.jpg
c:\inetpub\wwwroot\images\5003301.jpg

The 2nd one (imgpath)should say:
c:\inetpub\wwwroot\living_space_root\images\5003301.jpg

I have defined the local root folder and the testing server remote folder as:
c:\inetpub\wwwroot\living_space_root\
so why is it going back to wwwroot???? and not to living_space_root?????

Just so you are clear, my asp pages and database are in the living_space_root folder.
Also, as you can probably tell by the code and output above, the ImageLocation field within the database says:
images\5003301.jpg
kate perry is offline   Reply With Quote
Old 08-17-2004, 02:51 PM   PM User | #4
head8k
New Coder

 
Join Date: Jun 2002
Location: London & Oxford
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
head8k is an unknown quantity at this point
Here you go...

This should do the trick. I have defined a variable which lets you put in the name of the directory you are working in. Useful if you need to move the application at a later date.

Code:
<%
Dim objFSO, imagePath, workingDir

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

workingDir = "/living_space_root/"

imagePath = workingDir & Recordset1.Fields.Item("ImageLocation").Value

If objFSO.FileExists(server.mappath(imagePath)) Then
	Response.Write "<img src="""&imagePath&""" />"
Else
	Response.Write "<img src=""images/awaitingimage.jpg"" />"
End If
%>
__________________
As easy as 3.1415926535897932384626433832795028841
head8k is offline   Reply With Quote
Old 08-17-2004, 03:10 PM   PM User | #5
kate perry
New to the CF scene

 
Join Date: Aug 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
kate perry is an unknown quantity at this point
Thank you so much for your help - that was spot on!!!
kate perry is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:31 AM.


Advertisement
Log in to turn off these ads.