当前位置: 首页 > 学习 > 电脑学习 > 网站开发 > ASP > ASP技巧 > 正文

用ASP实现树形目录视图

http://www.zk168.com.cn  招考学习网 2006-4-30 14:13:05
-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--
Windows操作系统中的资源管理器是微软公司非常成功的文件管理软件。它的左边是整个文件系统的树形视图,右边是当前目录的文件(夹)列表,为文件管理提供了一个操作简单、使用方便的用户界面。但是,微软公司的IE浏览器却没有提供相类似的功能,如果能通过基于浏览器/Web服务器架构为用户提供一个与Windows 的资源管理器相类似的远程文件管理工具,将会极大地方便用户的使用。基于这种考虑,笔者用ASP技术实现了一个与 Windows 的资源管理器相类似的远程文件浏览器。本文介绍其中目录的树形视图、文件列表视图和文件搜索三个主要的部分的实现方法。
Default.asp文件
此文件是主文件,用来构造整个界面的框架。  
<html>
<head><title>欢迎光临</title></head>
<frameset rows=“80,20*” framespacing=“0”>
<frame name=“pMain” scrolling=“auto”noresize  
target=“contents” marginwidth=“1”marginheight=“1” src=“pMian.htm”>
<frameset cols=“230,530”>
<frame name=“pTreeView” src=“pTreeView.asp?
VFdPath=<%=Request(“VFdPath”)%>&Mflag=”scrolling=“1” marginwidth=“1” marginheight=“1” noresize auto“1”>
<frame name=“pListView” src=“pListView.asp?
VFdPath=<%=Request(“VFdPath”)%>” scrolling=“1” marginwidth=“1”marginheight=“1” noresize auto“1”>
</frameset><noframes><body>
<p>由于您的浏览器不支持此网页框架,所以您无法浏览此网页</p>
</body> </noframes></frameset></html>
pTreeView.asp文件
此文件主要负责目录树形视图的生成。其核心函数为FoldOpen(),负责响应用户对目录的操作,如打开一个文件夹等。三个参数的说明如下:
●sflups:当前的绝对虚拟路径;
●rsfd:当前物理路径;
●dep:当前路径深度(用来记载递归深度)。
在此函数中还调用了另一个函数UnMapPath(),它将返回物理路径所对应的虚拟路径(注:在本文的程序中,所有共享文件都被映射到虚拟目录gpdir下)。
<%
vfd = Request.QueryString(“VfdPath”)
Mflag = Request.QueryString(“Mflag”)
Server.ScriptTimeout = 500
On Error Resume Next
Set obfs = Server.CreateObject
(“Scripting.FileSystemObject”)
Set obfd=obfs.GetFolder(Server.MapPath(“/gpdir”) )
oblength = Len(obfd.path)
Function UnMapPath( Path )  
S=Mid( Path, oblength+1)
UnMapPath = Replace( S, “\”, “/”)
End Function
Function FoldOpen(sflups,rsfd,dep)  
sPic=“<img border=0 src="/Files/Pic/2006-4/30/0643014130352102.gif"””  
width=16 height=16>"  
if dep <> 0 then  
spib=“<a href=““pTreeView.asp?VFdPath=”  
& “/gpdir/” & UnMapPath(rsfd.Path)
else  
spib=“<a href=““pTreeView.asp?VFdPath=” & “/gpdir” & UnMapPath(rsfd.Path)
end if
spib=spib & “&Mflag=”
spib=spib & “c”” TARGET=““pTreeView””>”
Response.Write spib & sPic & “</a>”
sPic=“<img border=0 src="/Files/Pic/2006-4/30/0643014130464214.gif"”” width=16 height=16>” & rsfd.Name
spib=“<a href=““pListView.asp?VFdPath=”
if dep <> 0 then
spib=spib & “/gpdir/” & UnMapPath
(rsfd.Path) & “““ TARGET=””pListView””>”
else
spib=spib & “/gpdir” & UnMapPath(rsfd.Path) & “““ TARGET=””pListView””>”
end if
Response.Write spib & sPic & “</a></TD></TR>”
for each sfd in rsfd.SubFolders  
Response.Write “<TR><TD height=16 width=““100%””>”
for cn=0 to dep
Response.Write “<img border=0 src="/Files/Pic/2006-4/30/0643014130411323.gif"”” width=16 height=16>”
Next
if sfd.SubFolders.count>0 then
if InStr(1, sflups,“/gpdir/” & UnMapPath(sfd.Path), vbTextCompare) <> 1 then  
sPic=“<img border=0 src=““PIC/
pAdd.gif”” width=16 height=16>”
spib=“<a href=““pTreeView.asp?VFdPath=” & “/gpdir/” & UnMapPath(sfd.Path)  
spib=spib & “&Mflag=”
spib=spib & “o”””
spib=spib & “TARGET=““pTreeView””>”
Response.Write spib & sPic & “</a>”  
sPic=“<img border=0 src="/Files/Pic/2006-4/30/0643014130413161.gif"”” width=16 height=16>” & sfd.Name
Response.Write “<a href=““pListView
.asp?VFdPath=” & “/gpdir/” & UnMapPath(sfd.Path) & “““ TARGET=””pListView” ”>” & sPic & “</a></TD></TR>”  
else
scdep=dep+1
FoldOpen sflups, sfd, scdep
end if
else
Response.Write “<img border=0 src="/Files/Pic/2006-4/30/0643014130475023.gif"” ” width=16 height=16>”
sPic=“<img border=0 src="/Files/Pic/2006-4/30/0643014130413161.gif"”
”width=16 height=16>” & sfd.Name
Response.Write “<a href=““pListView.asp?
VFdPath=” & “/gpdir/” & UnMapPath(sfd.Path) & “““TARGET=””pListView””>” & sPic & “</a></TD></TR>”
end if  
next  
End Function
%>
<html>
<head><title>文件夹</title>
<base target=“main”></head>
<body bgcolor=“#008080” text=“#FFFFFF”>
<TABLE align=left border=0 cellPadding=0 cellSpacing=0 width=“100%”>
<TR> <TD width=“100%”>
<img border=“0” src="/Files/Pic/2006-4/30/064301413042962.gif"”>
</TD></TR><TR>  
<TD height=16 width=“100%”>  
<%  
if Request(“VFdPath”)=“” then  
FoldOpen “/gpdir”,obfd,0
else  
if Request(“Mflag”) =“c” then  
if Request(“VFdPath”) <> “/gpdir” Then
vfps=Request(“VFdPath”
Set scobfd = obfd.GetFolder( Server.MapPath(vfps) )  
obfd=scobfd.ParentFolder
FoldOpen “/gpdir/” & UnMapPath(refd),obfd,0
else  
sPic=“<img border=0 src=““PIC/
pAdd.gif”” width=16 height=16>”
spib=“<a href=““pTreeView.asp?VFdPath=/gpdir”
spib=spib & “&Mflag=”
spib=spib & “o” ” ”
spib=spib & “TARGET=““pTreeView””>”
Response.Write spib & sPic & “</a>”
sPic=“<img border=0 src=““PIC/
pClose.gif”” width=16 height=16>” & obfd.Name
Response.Write “<a href=““pListView.asp?VFdPath=/gpdir” ” TARGET=“”pListView””>” & sPic & “</a></TD></TR>”  
end if
else  
FoldOpen Request(“VFdPath”),obfd,0  
end if  
end if  
%>  
</table></body></html>
pListView.asp文件
该文件主要负责生成文件列表视图和响应用户在 该视图下的操作。限于篇幅,在此只给出其主要函数FileLink( f ),它用来生成文件 f 所对应的链接。
Function FileLink( f )  
FileLink = “<tr><td><A HREF=““” & “GetFl.asp?VFdPath=” & vfdir & “/” & f.Name & “&”
FileLink = FileLink & “Sz=” & f.Size\1024 & ““& ”””>”& f.Name & “</A> </td>”
FileLink = FileLink & “<td>” & f.Size\1024 & “KB</td>”
FileLink = FileLink & “<td>” & f.Type & “</td>”
FileLink = FileLink & “<td>” & Mid
(f.DateLastModified, 1,10) & “</td> </tr>”
End Function
SchEnd.asp文件
该文件负责响应文件搜索,并把搜索的结果用文件列表的形式显示给用户。由于当前目录是否有子目录、子目录共有多少层(即深度)都是不可知的,所以需要采用一种较好的算法,来实现子目录的层次显示和搜索。考虑到目录树本身的递归性,本文采用递归算法。搜索函数如下:
Sub SearchFolder( fd,fl)  
For each f In fd.Files
If InStr(1, f.Name,fl,vbTextCompare)>0 Then
Response.Write FileLink(f)
End If
Next
For each sfd In fd.SubFolders
SearchFolder sfd, fl
Next
End Sub
此函数在文件夹fd及其子文件夹下,递归搜索文件名中包含字符串fl的文件,并将其对应的链接显示给用户。
GetFl.asp文件
<%
vfd = Request.QueryString(“VfdPath”)
Response.Redirect vfd  
%>
GetFl.asp只有两行代码,之所以单独成为一个文件,是因为只要在此文件中增加适当的过滤代码,就可以为整个系统实现更多的功能,如:流量记录、下载文件统计等。
-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--
最新入库:
 
·10. 结束电话
·9 电话故障
·8. 分机、占线
·7. 打错电话
·6. 长途电话
·5. 转告消息
·4. 留 言
·3. 等候与回电
·2. 没找到人
·1. 打电话 接电话
相关内容:
 
·Wake-up Call Service 唤醒服务
·At the Barbers 在理发店与美容厅
·Maintenance 维修服务
·Settling complaints 处理投诉
·Laundry Service 洗衣服务
·At the Information Desk问讯处 2
·At the Information Desk问讯处 1
·The Bellman 应接服务员
·At the Reception Desk在接待处 2
·At the Reception Desk在接待处 1
网友点评:
 
会员名称:
密码:匿名 ·注册·忘记密码?
评论内容:
(最多300个字符)
  查看评论
友情提醒:
 1.库中的资料大都来自互联网、网友上传、各类书籍,在录入的过程中难免会出现错误,恳请网
 友来信指正!
 2.如果网友在本库中未能找到所需要的材料,请登陆到我们的论坛《招考学习网》版块!
 3.考友想加入招考学习网的编辑部,请发信到XueXiWang#Gmail.com(#改为@)附带个人简历
 4.如需转载请注明出处及作者,谢谢合作!
 5.如果您有更好的建议或意见请EMAIL:XueXiWang#Gmail.com (#改为@)
 6.凡标题中有注有“[NO]”字样均不含答案且答案整理中.
 7.如本库中转载文章涉及版权等问题,请相关网站或作者在两周内发邮件通知(EMAIL:  XueXiWang#Gmail.com (#改为@))我们,我们接到通知后立即删除该文章及链接!
你问我答 更多>>