views:

205

answers:

1

I am trying to set up a page that will allow the user to browse a file directory on the Web server.

The aim is to allow users to drop files within a given directory structure and the code will create the tree view based off the directory.

When setting the Nodes Navigate URL it maps to the C:\Staging\Files which obvioulsy does not work on the web. I would need to map to http://webaddress/staging/files etc

Here is the offending code

    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  A: 

I think you should take the reverse approach. Rather than getting contents of a physical path on disk, try using Server.MapPath to grab contents of a virtual path combine it with a base 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
Mehrdad Afshari
Could you give me an example of how this would work?
David A