Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  • #2
    New Coder
    Join Date
    Jun 2002
    Location
    London & Oxford
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    New Coder
    Join Date
    Jun 2002
    Location
    London & Oxford
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #5
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much for your help - that was spot on!!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •