[返回]
计算机世界2001年第11期

用ASP实现树形目录视图

华中理工大学 喻锋荣

  Windows操作系统中的资源管理器是微软公司非常成功的文件管理软件。它的左边是整个文件系统的树形视图,右边是当前目录的文件(夹)列表,为文件管理提供了一个操作简单、使用方便的用户界面。但是,微软公司的IE浏览器却没有提供相类似的功能,如果能通过基于浏览器/Web服务器架构为用户提供一个与Windows 的资源管理器相类似的远程文件管理工具,将会极大地方便用户的使用。基于这种考虑,笔者用ASP技术实现了一个与 Windows 的资源管理器相类似的远程文件浏览器。本文介绍其中目录的树形视图、文件列表视图和文件搜索三个主要的部分的实现方法。

程序运行的界面效果如图1所示。


图1 程序运行界面

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=““PIC/pSub.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=““PIC/pOpen.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 class=p9 height=16 width=““100%””>”

for cn=0 to dep

Response.Write “<img border=0 src=““PIC/pline.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=““PIC/pClose.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=““PIC/pmline.gif” ” width=16 height=16>”

sPic=“<img border=0 src=““PIC/pClose.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=“PIC/pFolder.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只有两行代码,之所以单独成为一个文件,是因为只要在此文件中增加适当的过滤代码,就可以为整个系统实现更多的功能,如:流量记录、下载文件统计等。