This is a migrated thread and some comments may be shown as answers.

treeview upload

2 Answers 44 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Bryan Davis
Top achievements
Rank 1
Bryan Davis asked on 10 Aug 2009, 01:35 PM
Hello everybody, I have posted this question a few times on some different forums with know luck.  I am trying to upload a file to a specific folder based on treeview node selection.  Here is my code.  Can anybody help?  Thanks

Imports System.IO
Imports Telerik.Web.UI

 

Partial Public Class uploadfiles
    Inherits System.Web.UI.Page

   
  

    Protected Sub UploadButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles UploadButton.Click
        BindResults()
    End Sub

    Private Sub BindTreeToDirectory(ByVal dirPath As String, ByVal parentNode As RadTreeNode)
        Dim dirs As String() = Directory.GetDirectories(dirPath)
        Dim s As String
        For Each s In dirs

            Dim parts As String() = s.Split("\"c)
            Dim name As String = parts((parts.Length - 1))
            If name <> "Examples" Then
                Dim node As New RadTreeNode(name)
                node.ImageUrl = "Folder.gif"
                node.Value = name
                node.Category = "Folder"
                parentNode.Nodes.Add(node)

                BindTreeToDirectory(s, node)
            End If
        Next s

        Dim files As String() = Directory.GetFiles(dirPath)
        Dim fileNAme As String
        For Each fileNAme In files
            Dim parts As String() = fileNAme.Split("\"c)
            Dim name As String = parts((parts.Length - 1))
            Dim node As New RadTreeNode(name)
            Dim fi As New FileInfo(fileNAme)
            node.ImageUrl = "File.gif"
            node.Value = fi.Name & "@" & fi.Length.ToString() & "@" & fi.LastWriteTime.ToString()
            node.Category = dirPath & "\" & fi.Name

            parentNode.Nodes.Add(node)
        Next fileNAme
    End Sub

    Private Sub BindResults()
        Dim resultsExist As Boolean = RadUpload1.UploadedFiles.Count > 0
        lblNoResults.Visible = Not resultsExist
        rptReqults.Visible = resultsExist
        If resultsExist Then
            rptReqults.DataSource = RadUpload1.UploadedFiles
            rptReqults.DataBind()
        End If
      
        Dim rootFolder As String = Server.MapPath("~/filefolders/")
        Dim rootNode As New RadTreeNode(rootFolder)
        RadTree1.Nodes.Clear()
        rootNode.Text = "Software"
        rootNode.Value = Server.MapPath("~/filefolders/")
        rootNode.ImageUrl = "Folder.gif"
        rootNode.Category = "Folder"
        rootNode.Expanded = True
        RadTree1.Nodes.Add(rootNode)
        BindTreeToDirectory(rootFolder, rootNode)
        Dim uploadnode As RadTreeNode = RadTree1.FindNodeByText("RadTree1.SelectedNode.Text")
        uploadnode.Expanded = True
  End Sub

    Private Sub InitializeComponent()
        Dim nd As New DirectoryInfo(Server.MapPath(".") & "/filefolders/" & RadTree1.SelectedNode.Text & "/")
        If nd.Exists = False Then
            nd.Create()
        End If
        RadUpload1.TargetFolder = "~/filefolders/" & RadTree1.SelectedNode.Text & "/"

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            'Dim rootFolder As String = ConfigurationManager.AppSettings("ParentFolderPath")
            Dim rootFolder As String = Server.MapPath("~/filefolders/")
            Dim rootNode As New RadTreeNode(rootFolder)
            rootNode.Text = "Santricity"
            rootNode.Value = Server.MapPath("~/filefolders/")
            rootNode.ImageUrl = "Folder.gif"
            rootNode.Category = "Folder"
            rootNode.Expanded = True
            RadTree1.Nodes.Add(rootNode)
            BindTreeToDirectory(rootFolder, rootNode)
            'RadUpload1.TargetFolder = "~/filefolders/" & ("Linux") & "/"

        End If

    End Sub
End Class

2 Answers, 1 is accepted

Sort by
0
Genady Sergeev
Telerik team
answered on 17 Aug 2009, 09:02 AM
Hi Bryan Davis,

Can you clarify what your exact problem is? The easiest way to this is to hook on the NodeClick server event and set the TargetFolder property based on the value of the clicked node.

Regards,
Genady Sergeev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Bryan Davis
Top achievements
Rank 1
answered on 17 Aug 2009, 12:56 PM

Thank you but I have solved the issue.  Here is my code incase anybody else needs this.

Thank you.

  Public Property CurrentDir() As DataTable  
        Get 
            Return CType(ViewState(ConfigurationManager.AppSettings("ParentFolderPath")), DataTable)  
        End Get 
        Set(ByVal value As DataTable)  
            ViewState(ConfigurationManager.AppSettings("ParentFolderPath")) = value  
        End Set 
 
    End Property 
 
 
 
    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load  
        If Not IsPostBack Then 
            LoadTreeView(Server.MapPath("~/filefolders"))  
            LoadGrid(Server.MapPath("~/filefolders"))  
        End If 
    End Sub 
 
    Private Sub LoadTreeView(ByVal folder As String)  
        Dim rootNode As New RadTreeNode("Santricity")  
        rootNode.ImageUrl = "Folder.gif" 
        rootNode.Expanded = True 
        rootNode.Category = "Folder" 
        rootNode.Value = folder  
        RadTree1.Nodes.Add(rootNode)  
        CurrentDir = GetFileList(folder)  
        BindDirectory(folder, rootNode.Nodes)  
    End Sub 
 
 
    Private Sub BindDirectory(ByVal dirPath As StringByVal collection As RadTreeNodeCollection)  
        If dirPath = String.Empty Then 
            Return 
 
        End If 
        Dim dirs As String() = Directory.GetDirectories(dirPath)  
        Dim path As String 
        For Each path In dirs  
            Dim parts As String() = path.Split("\"c)  
            Dim name As String = parts((parts.Length - 1))  
            Dim node As New RadTreeNode(name)  
            node.ImageUrl = "Folder.gif" 
            node.Value = path  
            node.Category = "Folder" 
 
            If Directory.GetDirectories(path).Length > 0 Then 
                node.ExpandMode = TreeNodeExpandMode.ServerSide  
            End If 
            collection.Add(node)  
        Next path  
 
    End Sub 
 
    Private Sub LoadGrid(ByVal directory As String)  
        Dim filesAndFolders As New DataTable()  
        filesAndFolders.Columns.Add("Name")  
        filesAndFolders.Columns.Add("Size")  
 
        If directory <> String.Empty Then 
 
            Dim dir As New DirectoryInfo(directory)  
 
            Dim subDir As DirectoryInfo  
            For Each subDir In dir.GetDirectories()  
                filesAndFolders.Rows.Add(New String() {subDir.Name, " "})  
            Next subDir  
 
            Dim file As FileInfo  
            For Each file In dir.GetFiles()  
                filesAndFolders.Rows.Add(New String() {file.Name, file.Length.ToString()})  
            Next file  
        End If 
 
        RadGrid1.DataSource = filesAndFolders  
 
    End Sub 
    Private Function GetFileList(ByVal directory As StringAs DataTable  
        Dim filesAndFolders As New DataTable()  
        filesAndFolders.Columns.Add("Name")  
        filesAndFolders.Columns.Add("Size")  
 
        If directory <> String.Empty Then 
            Dim dir As New DirectoryInfo(directory)  
            Dim subDir As DirectoryInfo  
            For Each subDir In dir.GetDirectories()  
                filesAndFolders.Rows.Add(New String() {subDir.Name, "-1"})  
 
            Next subDir  
 
            Dim file As FileInfo  
            For Each file In dir.GetFiles()  
                filesAndFolders.Rows.Add(New String() {file.Name, file.Length.ToString()})  
 
            Next file  
        End If 
        Return filesAndFolders  
 
    End Function 
 
    Protected Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As GridItemEventArgs)  
        If TypeOf e.Item Is GridDataItem Then 
            Dim icon As System.Web.UI.WebControls.Image = CType(e.Item.FindControl("icon"), System.Web.UI.WebControls.Image)  
            icon.ImageUrl = "File.png" '"Img/" + GetImageForExtension(CStr(DataBinder.Eval(e.Item.DataItem, "Name")))  
            icon.AlternateText = "icon" 
            icon.ImageAlign = System.Web.UI.WebControls.ImageAlign.AbsMiddle  
            icon.Style.Add("vertical-align""middle")  
            icon.BorderWidth = Unit.Pixel(0)  
 
            Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)  
 
            Dim fileLength As String = CStr(DataBinder.Eval(e.Item.DataItem, "Size"))  
            If fileLength <> "-1" Then 
                dataItem("Size").Text = fileLength + " kbs" 
 
            Else 
                dataItem("Size").Text = " " 
            End If 
        End If 
    End Sub 'RadGrid1_ItemDataBound  
 
 
    Protected Sub RadGrid1_NeedDataSource1(ByVal [source] As ObjectByVal e As GridNeedDataSourceEventArgs)  
        If Not (RadTree1.SelectedNode Is NothingThen 
            LoadGrid(RadTree1.SelectedNode.Value)  
        Else 
            LoadGrid(Server.MapPath("~/"))  
        End If 
    End Sub 'RadGrid1_NeedDataSource1  
 
 
    Protected Sub RadTreeView1_NodeExpand(ByVal o As ObjectByVal e As RadTreeNodeEventArgs)  
        If e.Node.Nodes.Count = 0 Then 
            BindDirectory(e.Node.Value, e.Node.Nodes)  
        End If 
    End Sub 
 
    Protected Sub RadTreeView1_NodeClick(ByVal o As ObjectByVal e As RadTreeNodeEventArgs)  
        SelectNode(e.Node)  
    End Sub 
 
 
    Protected Sub SelectNode(ByVal selectedNode As RadTreeNode)  
        CurrentDir = GetFileList(selectedNode.Value)  
 
        If selectedNode.Nodes.Count = 0 Then 
            BindDirectory(selectedNode.Value, selectedNode.Nodes)  
        End If 
 
        RadGrid1.DataSource = CurrentDir  
        RadGrid1.Rebind()  
        Dim uploadnode As String = RadTree1.SelectedNode.Value  
 
    End Sub 'SelectNode  
 
    Protected Sub UploadButton_Click(ByVal sender As ObjectByVal e As EventArgs) Handles UploadButton.Click  
        For Each f As UploadedFile In RadUpload1.UploadedFiles  
            f.SaveAs(RadTree1.SelectedNode.Value & "\" & f.GetName(), True)  
            RadGrid1.Rebind()  
        Next 
    End Sub 
Tags
TreeView
Asked by
Bryan Davis
Top achievements
Rank 1
Answers by
Genady Sergeev
Telerik team
Bryan Davis
Top achievements
Rank 1
Share this question
or