Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do not show Image if image is not availabole

Status
Not open for further replies.

bassguy

Programmer
Jun 21, 2001
336
US
Hi, I asked last week but I do not think I asked correctly. I am going to try again. I am trying to show an image in an area but I do not know if the image actually exists. I want to do an if FileExists then show file. I have seen the code on Msdn and either am using bad code or I do not understand it. So, How do I query an Image directory to see if a specific file exists?

any help would be appreciated.


thanks

bassguy
 
you're right - you should use file exists.

here's a snippet of what i did at work adapt as you see fit:

if you have any questions, post back:

Code:
'Function showImg(imagefile, sku)
Function showImg(sku)
 dim sNum, imgStr, imgStr2, imageFile2
 'if imageFile is null or imageFile = "" or imageFile = 0 or imageFile = "null" then
	sNum = left(sku,1)
	imgStr  = Server.MapPath("images\" &sNum& "\" &sku& ".gif")
	imageFile = sku & ".gif"
	imgStr2 = Server.MapPath("images\" &sNum& "\" &sku& ".jpg")
	imageFile2 = sku & ".jpg"
' else 
	'sNum = left(imagefile,1)
	'imgStr = Server.MapPath("images\" &sNum& "\" &imagefile)
	'imgStr2 = imgStr
	'imageFile2 = imageFile
'end if
 
 'check for image existance
 dim fsoObj
 set fsoObj = server.CreateObject("Scripting.FileSystemObject")
 if fsoObj.FileExists(imgStr) then
	set fsoObj = nothing
	showImg = &quot;<img src=&quot;&quot;images\&quot; &sNum& &quot;\&quot; &imageFile & &quot;&quot;&quot; border=&quot;&quot;0&quot;&quot;>&quot;
 elseif fsoObj.FileExists(imgStr2) then
	set fsoObj = nothing
	showImg = &quot;<img src=&quot;&quot;images\&quot; &sNum& &quot;\&quot; &imageFile2 & &quot;&quot;&quot; border=&quot;&quot;0&quot;&quot;>&quot;
 else
	set fsoObj = nothing
	showImg = &quot;<img src=&quot;&quot;images\client_request\img_na.gif&quot;&quot; border=&quot;&quot;0&quot;&quot; alt=&quot;&quot;Sorry, image not available&quot;&quot;>&quot;
 end if
end Function

you might be wondering why i set fsoObj = nothing three times... this was because i wasn't sure whether or not the deallocation of fsoObj would occur after the end if. the showImg = ... implied that it would jump out of the function, and the fsoObj would not get explicitly set to nothing.

hth
leo
 
There u go

<%
Const ForReading = 1, ForWriting = 2, ForAppending = 8
set FSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)

GetImage = filePath
set myFile = FSO.GetFile(GetImage)
%>
<html>
<head>
<title>GetImage IfExists</title>
</head>
<body>
<%
response.write &quot;<img src=&quot;&quot;&quot;&myfile&&quot;&quot;&quot;>&quot;
Response.Write &quot;<b>&quot; &myfile& &quot;</b>&quot;
%>
</body>
</html>

<%
function filePath
filepath = request.servervariables(&quot;PATH_TRANSLATED&quot;)
blnFound = True
icount = 1 'start at the beguining of the string

While blnfound = True
'find a \ in the file path
if InStr(icount,filepath,&quot;\&quot;) >= 1 Then
'set the counter to look for another &quot;\&quot; after the one it has just found
icount = instr(icount,filepath,&quot;\&quot;) + 1
else
' no more &quot;\&quot;
blnfound = False
End If
wend

'start at the begining and grab everything up to the last &quot;\&quot;
filename = Mid(filepath,1,icount - 1) & &quot;jacob.jpg&quot;
filePath = filename
end function
%>
gsc1ugs
&quot;Cant see wood for tree's...!&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top