0

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();  


 }






 }
1

Chrome, Firefox and most of the other browsers does not allow opening shared/network folder files due to security reasons.

The workaround is given on below link:https://stackoverflow.com/questions/9152792/link-to-a-network-location-using-file-does-not-work-in-chrome

It says,

  1. Create another web server using IIS or Apache
  2. Point it to the network directory
  3. And then In your webpart give links to files using HTTP
  • My network file server is ibm i series and the share path is // instead of \\ virtual directory \\root\direct while ibm iseries //root/direct – user17970 Jul 22 '15 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.