2009-07-11 71 views
0

我想設置一個頁面,將允許用戶瀏覽Web服務器上的文件目錄。樹視圖文件到Web地址而不是目錄地址

其目的是允許用戶將文件放在給定的目錄結構中,代碼將根據目錄創建樹視圖。

當設置節點導航URL時,它將映射到C:\ Staging \ Files文件,該文件在Web上不起作用。我需要映射到http://webaddress/staging/files

這裏是有問題的代碼

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load 


    If Not Page.IsPostBack Then 
     ' 
     Dim rootDir As New DirectoryInfo("C:\Staging\") 
     ' Enter the RecurseNodes function to recursively walk the directory tree. 
     Dim RootNode As TreeNode = RecurseNodes(rootDir) 
     ' Add this Node hierarchy to the TreeNode control. 
     Treeview1.Nodes.Add(RootNode) 
    End If 
End Sub 

Private Function RecurseNodes(ByVal thisDir As DirectoryInfo) As TreeNode 


    Dim thisDirNode As New TreeNode(thisDir.Name, Nothing) 
    ' Get all the subdirectories in this Directory. 
    Dim subDirs As DirectoryInfo() = thisDir.GetDirectories() 
    For Each subDir As DirectoryInfo In subDirs 
     thisDirNode.ChildNodes.Add(RecurseNodes(subDir)) 
    Next 
    ' Now get the files in this Directory. 
    Dim files As FileInfo() = thisDir.GetFiles() 
    For Each file As FileInfo In files 
     Dim thisFileNode As New TreeNode(file.Name, Nothing) 
     **thisFileNode.NavigateUrl = file.FullName** 
     thisDirNode.ChildNodes.Add(thisFileNode) 
    Next 
    Return thisDirNode 
End Function 

回答

1

我想你應該採取相反的方法。而不是讓磁盤上的物理路徑的內容,請嘗試使用Server.MapPath搶虛擬路徑的內容與基本URL結合起來:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    If Not Page.IsPostBack Then 
     Dim RootNode As TreeNode = RecurseNodes("~/files") 
     TreeView1.Nodes.Add(RootNode) 
    End If 
End Sub 

Private Overloads Function RecurseNodes(ByVal virtualPath As String) As TreeNode 
    If Not VirtualPathUtility.IsAbsolute(virtualPath) Then virtualPath = VirtualPathUtility.ToAbsolute(virtualPath) 
    virtualPath = VirtualPathUtility.RemoveTrailingSlash(virtualPath) 
    Dim baseUrl As String = Request.Url.GetLeftPart(UriPartial.Authority) + virtualPath 
    Return RecurseNodes(New DirectoryInfo(Server.MapPath(virtualPath)), baseUrl) 
End Function 

Private Overloads Function RecurseNodes(ByVal thisDir As DirectoryInfo, ByVal baseUrl As String) As TreeNode 
    Dim thisDirNode As New TreeNode(thisDir.Name, Nothing) 
    Dim subDirs As DirectoryInfo() = thisDir.GetDirectories() 
    For Each subDir As DirectoryInfo In subDirs 
     thisDirNode.ChildNodes.Add(RecurseNodes(subDir, baseUrl + subDir.Name + "/")) 
    Next 
    Dim files As FileInfo() = thisDir.GetFiles() 
    For Each file As FileInfo In files 
     Dim thisFileNode As New TreeNode(file.Name, Nothing) 
     thisFileNode.NavigateUrl = baseUrl + file.Name 
     thisDirNode.ChildNodes.Add(thisFileNode) 
    Next 
    Return thisDirNode 
End Function 
+0

可不可以給我這是如何工作的例子嗎? – 2009-07-12 15:35:37

相關問題