我有一个在 IIS Express 上运行的 WebForms 应用程序。
我使用
directoryBrowse=enabled
让用户访问部分文件结构以下载和打开文件。
当我访问网站上的公共目录时,会像往常一样显示标准的 IIS 目录列表页面,其中包含目录内容。
我想注册一个专门用于目录浏览的自定义处理程序,以控制浏览目录时显示的内容以及方式。我知道如何为特定文件或文件类型注册处理程序,但是我不知道如何为目录注册处理程序(最好是全局任何目录的相同处理程序)。
嗯,关闭文件夹浏览,然后说提供您自己的基于 Web 的 UI 不是更好吗?
我注意到上面的内容,因为这个 VAST 提高了安全性,允许无限制地定制 UI。并表示允许根据用户的登录限制文件夹和文件。
更好的是,您还可以使用服务器以外的文件夹,但不必创建虚拟文件夹并将其从 Web 服务器映射到其他服务器。
这意味着,对于其他文件夹等,您无需更改 Web 服务器配置。
假设您将树视图控件放入网页。
选择图像集为“windows xp file explore”。
所以,现在我们有了这个标记:
<asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer"
NodeIndent="15" OnTreeNodeExpanded="TreeView1_TreeNodeExpanded1">
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black"
HorizontalPadding="2px" NodeSpacing="0px" VerticalPadding="2px" />
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False"
HorizontalPadding="0px" VerticalPadding="0px" />
</asp:TreeView>
因此,“浏览”文件夹的代码并不是很多代码,但是您当然可以引入对文件类型的限制,并将文件夹(限制)基于用户的登录。 所以,说这段代码: (甚至不是递归,也不需要递归)。
Dim sRoot As String = "c:\SERVER01"
Public Class MyFolder
Public MyFiles As FileInfo()
Public MyFolders As DirectoryInfo()
End Class
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
lblFolder.Text = sRoot
Dim MyFiles As MyFolder = GetFiles(sRoot)
LoadTreeFiles(sRoot, MyFiles, "", Nothing)
End If
End Sub
Public Function GetFiles(sRootFolder As String) As MyFolder
Dim MyDir As New DirectoryInfo(sRootFolder)
Dim cMyFolder As New MyFolder
cMyFolder.MyFolders = MyDir.GetDirectories("*.*", SearchOption.TopDirectoryOnly)
cMyFolder.MyFiles = MyDir.GetFiles("*.*", SearchOption.TopDirectoryOnly)
Return cMyFolder
End Function
Sub LoadTreeFiles(fRootL As String,
lParent As MyFolder,
sParentID As String,
tTreeNode As TreeNode)
' add folders if any
For Each sFolder As DirectoryInfo In lParent.MyFolders
Dim child As New TreeNode
child.Text = sFolder.Name
child.Value = sFolder.FullName
child.Expanded = False
child.PopulateOnDemand = True
child.ShowCheckBox = False
If sParentID = "" Then
TreeView1.Nodes.Add(child)
Else
tTreeNode.ChildNodes.Add(child)
End If
Next
' now any files
For Each sFile As FileInfo In lParent.MyFiles
Dim child As New TreeNode
child.Text = sFile.Name
child.Value = sFile.FullName
child.Expanded = False
child.ShowCheckBox = True
child.PopulateOnDemand = False
If sParentID = "" Then
TreeView1.Nodes.Add(child)
Else
tTreeNode.ChildNodes.Add(child)
End If
Next
End Sub
Protected Sub TreeView1_TreeNodeExpanded1(sender As Object, e As TreeNodeEventArgs) Handles TreeView1.TreeNodeExpanded
Dim child As TreeNode = e.Node
Dim dtChild As MyFolder = GetFiles(child.Value)
LoadTreeFiles(child.Value, dtChild, child.Text, child)
End Sub
所以,现在网页显示:
因此,尚不清楚您是否需要/想要一个自定义处理程序,而只是使用树视图控件构建某种 UI,并按照上面的方式构建您自己的文件浏览系统。由于我只是“按需”加载每个文件夹,因此上述速度非常快,即使对于具有大而深的文件夹层次结构的系统也是如此。
上面有用于选择多个文件的复选框,但您可以单击文件下载。 上述设计允许您添加额外的 UI 选项,例如“选择所有此文件夹”或其他任何选项。