Treeview webpart to display network folders and files

I am developing a webpart which will display folders and files from a netwprl drive as a treeview and I am able to do that in a webpart and when the user clicks on a file it either opens in the browser or prompts the user to download it  only catch is it works only in Internet explorer , it does not work in chrome or Firefox 

here is my code 

I create a visual web part and then add this code to the ascx.cs page 

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BuildTree();
        }
    }

    protected void BuildTree()
    {
        tvFiles.Nodes.Clear();
        DirectoryInfo di = new DirectoryInfo(@"\\dev\root\supp_web");
      
        TreeNode root = new TreeNode(di.Name, di.FullName);

     

        root.SelectAction = TreeNodeSelectAction.Select;
        root.PopulateOnDemand = true;
        root.Selected = false;
        tvFiles.Nodes.Add(root);
        tvFiles.ExpandDepth = 0;
       // root.Expand();

    


    }
    protected void tvFiles_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        TreeNode parentNode = e.Node;

        if (parentNode.Depth == 0)
        {
            PopulateDrives(parentNode);
        }

        else
        {
            PopulateNormal(parentNode);

        }


    }

    private void PopulateNormal(TreeNode node)
    {
        string path = node.Value;

        if (node.Depth == 1)
        {
            path = node.ValuePath;
        }
        else
        {
            path = node.Value;
        }

        try
        {
            DirectoryInfo directory = new DirectoryInfo(path); 
           // DirectoryInfo directory = new DirectoryInfo(path);

            foreach (FileSystemInfo child in directory.GetFileSystemInfos())
            {
                TreeNode childNode = new TreeNode();
                childNode.Text = child.Name;
                childNode.Value = child.FullName;
                

                if (child is DirectoryInfo)
                {
                   
                    if (((DirectoryInfo)child).GetFileSystemInfos().Length > 0)
                    {
                        childNode.PopulateOnDemand = true;
                        childNode.SelectAction = TreeNodeSelectAction.None;
                        //childNode.NavigateUrl = "#";
                    }
                    
                }
                else
                {
                   childNode.SelectAction = TreeNodeSelectAction.SelectExpand;
                   childNode.PopulateOnDemand = false;
                   childNode.Text = child.Name;// +"<input type='Button' name='open'/>";
                    
                }
   
                node.ChildNodes.Add(childNode);
               
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }




    private void PopulateDrives(TreeNode node)
    {
          string path = node.Value;
          
          DirectoryInfo dirInfo = new DirectoryInfo(path);

          foreach (DirectoryInfo sub in dirInfo.GetDirectories())
          {
              TreeNode driveNode = new TreeNode(sub.Name, sub.Name);
              driveNode.SelectAction =
                       TreeNodeSelectAction.None;
              driveNode.Selected = false;
              driveNode.PopulateOnDemand = true;

              node.ChildNodes.Add(driveNode);

          }


          
        }


    }

    // this event is called just to accomodate Firefox and Chrome 

    protected void tvFiles_SelectedNodeChanged(object sender, EventArgs e)
    {
        Response.Write(tvFiles.SelectedNode.Text);
        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        response.ClearContent();
        response.Clear();
        response.ContentType = "text/plain";
        response.AddHeader("Content-Disposition", "attachment; filename=" + tvFiles.SelectedNode.Text + ";");
        response.TransmitFile(tvFiles.SelectedNode.Value);
        response.Flush();
        response.End();  


    }


   


   
}


July 21st, 2015 7:22pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics