◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
ASP实现随机显示图片
演示地址:点击(然后刷新看看)。
代码如下:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
Function AllPath()
Dim Domain,GFilePath
Domain = Request.ServerVariables("SERVER_NAME")
GFilePath = Request.ServerVariables("PATH_INFO")
GFilePath = lcase(left(GFilePath,instrRev(GFilePath,"/")))
AllPath = "http://"&Domain&GFilePath
End Function
Function ShowFileList(folderspec)
Dim Path,objFSO,objFolder,count,objFile,nume,S
Path = Server.MapPath(folderspec)
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(Path) Then
Set objFolder = objFSO.GetFolder(Path)
count = 0
For Each objFile in objFolder.Files
count = count+1
Next
randomize
nume = Int((count*rnd)+1)
S = 0
ShowFileList = ""
For Each objFile in objFolder.Files
S = S + 1
If S = nume Then
ShowFileList = objFile.Name
Exit For
End If
Next
Set objFolder = Nothing
Else
ShowFileList = "NO"
End If
Set objFSO = Nothing
End Function
Dim list,filename,address,str
list = trim(Request.QueryString("list"))
if list = "" then
Response.write "本页需要正确参数引入,您缺少相关的参数!正确格式如下:"&AllPath&"img.asp?list=图片存放目录"
Response.End()
end if
filename = ShowFileList("./"&list&"/")
if filename = "NO" then
Response.write "您指定的目录<b>"&list&"</b>不存在,请重新指定!"
Response.End()
end if
if filename = "" then
Response.write "您指定的目录<b>"&list&"</b>没有相关的图片文件存在,请重新指定!"
Response.End()
end if
str = right(filename,3)
if str<>"jpg" and str<>"gif" then
filename = "erro.gif"
end if
address = AllPath&list&"/"
address = address&filename
%>
<%Response.redirect(address)%>
使用说明:
1.上述代码存为img.asp(可自定义,但请修改以下相关项)
2.调用方法
<img src="img.asp?list=你存放图片的路径">
3.asp文件和图片路径并列于同一目录下。4.上述代码中的
if str<>"jpg" and str<>"gif" then
指定了图片的格式,可自行根据情况增删判断语句。5.本代码可自由使用。
|
|
||
|
|
|
|
![]() |
||
发布:Debugger | 分类:代码收集 | 引用本文 | 发表评论 | 返回顶部
- 原创文章如转载请注明:来自[闻思家园]:http://www.winshome.com/coding/2006-12/144.html
