472,128 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,128 software developers and data experts.

Show image randomly

2
It run w/o error but no image appears. Please help me. Here si the code I do
"
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>

<body>
<%
Function RandomImage(ImageFolderPath, ImageFileTypes, ImageDescription)
'Declare variables
Dim CompleteImagesFolderPath
Dim FileSystemObject
Dim ImageFolder
Dim Files
Dim i
Dim ImageFiles
Dim File
Dim FileName
Dim FileExtension
Dim RandomNumber

'Find the complete path to image folder by using Server.MapPath
CompleteImagesFolderPath = Server.MapPath(ImageFolderPath)

'Create an instance of the FileSystemObject which allows ASP to access the file system
Set FileSystemObject = Server.CreateObject("Scripting.FileSystemObject")

'Check that the folder containing the images exists
If Not FileSystemObject.FolderExists(CompleteImagesFolder Path) Then
RandomImage = "Error 0: Cannot find requested folder"
Set FileSystemObject = nothing
Exit Function
End If

'Get the folder containing the images
Set ImageFolder = FileSystemObject.GetFolder(CompleteImagesFolderPat h)

'Get a list of all the files within the images folder
Set Files = ImageFolder.Files

'Use a dictionary object to temporarily store the image file names
i = 1
Set ImageFiles = Server.CreateObject("Scripting.Dictionary")

'Loop through the list of files within the images folder.
'If the file has a file extension that is in the list of
'file types specified in the ImageFileTypes function parameter,
'then add the file name to the ImageFiles dictionary object
For Each File in Files
FileName = File.Name
FileExtension = Right(FileName, Len(FileName) - (InStrRev(FileName, ".")))

If InStr(1,ImageFileTypes,FileExtension,vbTextCompare ) > 0 then
ImageFiles.Add i, FileName
i = i + 1

End If

Next

'Destroy objects that are no longer required
Set ImageFolder = nothing
Set Files = nothing
Set FileSystemObject = nothing

'Initialise the random number generator
Randomize

' Check that image file(s) have been found
If ImageFiles.Count = 0 Then
RandomImage = "Error 1: Requested folder does not contain any image files"
Exit Function
End If

'Generate a random number between 1 and the number of image files
RandomNumber = Int((ImageFiles.Count) * Rnd + 1)

'Return a hyperlink to a random image file
RandomImage = "<img src=" & Chr(34) & ImagesFolderPath & ImageFiles.Item(RandomNumber) & Chr(34) & " border=" & Chr(34) & "3" & Chr(34) & " width=" & Chr(34) & "450" & Chr(34) & " height=" & Chr(34) & "600" & Chr(34) & " alt=" & Chr(34) & ImageDescription & Chr(34) & ">"

Set ImageFiles = nothing

End Function
%>

<%Response.Write RandomImage("Chi/", "jpg", "My Image")%>

<p>&nbsp;</p>
</body>

"
Nov 13 '06 #1
1 1854
sashi
1,754 Expert 1GB
It run w/o error but no image appears. Please help me. Here si the code I do
"
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>

<body>
<%
Function RandomImage(ImageFolderPath, ImageFileTypes, ImageDescription)
'Declare variables
Dim CompleteImagesFolderPath
Dim FileSystemObject
Dim ImageFolder
Dim Files
Dim i
Dim ImageFiles
Dim File
Dim FileName
Dim FileExtension
Dim RandomNumber

'Find the complete path to image folder by using Server.MapPath
CompleteImagesFolderPath = Server.MapPath(ImageFolderPath)

'Create an instance of the FileSystemObject which allows ASP to access the file system
Set FileSystemObject = Server.CreateObject("Scripting.FileSystemObject")

'Check that the folder containing the images exists
If Not FileSystemObject.FolderExists(CompleteImagesFolder Path) Then
RandomImage = "Error 0: Cannot find requested folder"
Set FileSystemObject = nothing
Exit Function
End If

'Get the folder containing the images
Set ImageFolder = FileSystemObject.GetFolder(CompleteImagesFolderPat h)

'Get a list of all the files within the images folder
Set Files = ImageFolder.Files

'Use a dictionary object to temporarily store the image file names
i = 1
Set ImageFiles = Server.CreateObject("Scripting.Dictionary")

'Loop through the list of files within the images folder.
'If the file has a file extension that is in the list of
'file types specified in the ImageFileTypes function parameter,
'then add the file name to the ImageFiles dictionary object
For Each File in Files
FileName = File.Name
FileExtension = Right(FileName, Len(FileName) - (InStrRev(FileName, ".")))

If InStr(1,ImageFileTypes,FileExtension,vbTextCompare ) > 0 then
ImageFiles.Add i, FileName
i = i + 1

End If

Next

'Destroy objects that are no longer required
Set ImageFolder = nothing
Set Files = nothing
Set FileSystemObject = nothing

'Initialise the random number generator
Randomize

' Check that image file(s) have been found
If ImageFiles.Count = 0 Then
RandomImage = "Error 1: Requested folder does not contain any image files"
Exit Function
End If

'Generate a random number between 1 and the number of image files
RandomNumber = Int((ImageFiles.Count) * Rnd + 1)

'Return a hyperlink to a random image file
RandomImage = "<img src=" & Chr(34) & ImagesFolderPath & ImageFiles.Item(RandomNumber) & Chr(34) & " border=" & Chr(34) & "3" & Chr(34) & " width=" & Chr(34) & "450" & Chr(34) & " height=" & Chr(34) & "600" & Chr(34) & " alt=" & Chr(34) & ImageDescription & Chr(34) & ">"

Set ImageFiles = nothing

End Function
%>

<%Response.Write RandomImage("Chi/", "jpg", "My Image")%>

<p>&nbsp;</p>
</body>

"
Hi there,

Kindly refer to the links attached below, make proper modification to your existing code, hope it helps. Good luck & take care.

http://www.asp101.com/samples/viewas...ndom_image.asp
http://www.codeave.com/asp/code.asp?u_log=85
Nov 13 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Ronny | last post: by
9 posts views Thread by Michael Burtenshaw | last post: by
reply views Thread by mhospodarsky | last post: by
2 posts views Thread by bedges | last post: by
4 posts views Thread by tshad | last post: by
1 post views Thread by rsteph | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.