Welcome to my ASP Code Website

Handling Missing Graphic Files



It's common to use ASP to display graphic images on the fly. You can have your code check to make sure the graphic file is actually there - and show a default image if it is missing for some reason.

The command you're going to use here is FileExists - the command checks to see if the file you specify actually exists on the server. You can then do different processing based on whether or not you find the specified file.

Let's say you have an inventory system where all of your products are given sequential numbers - from P1 to P1000 and so on. Your image files are named to match, so P1.jpg, P2.jpg, etc. This normally makes it very easy for you to automatically show the image that goes with a given product.

If you have to remove an image for some reason, the following code will show a default image until you get around to putting a new proper image into the directory.

<%
dim picFSO
set picFSO = createobject("Scripting.FileSystemObject")
PicPath = "/images/products/P" & ProductID & ".jpg"
PicFullPath = Server.MapPath(PicPath)
if picFSO.FileExists(PicFullPath) THEN
FoundImage = TRUE
else
FoundImage = FALSE
end if
%>

Once you've got that FoundImage set properly, you can now do whatever if-then statements you like. You can use a standard IMG SRC= to display that proper PicPath entry if FoundImage is true, or show another default entry if the FoundImage is false.

ASP Utility Code