...

View Full Version : Displaying alternate image when image source not found



kate perry
08-17-2004, 10:37 AM
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
%>

head8k
08-17-2004, 02:15 PM
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?

kate perry
08-17-2004, 02:31 PM
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

head8k
08-17-2004, 02:51 PM
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.




<%
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
%>

kate perry
08-17-2004, 03:10 PM
Thank you so much for your help - that was spot on!!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum