0

Hello

I have searched a lot in Google but I could not find the clear answer.

I have used below code for structure of folders and files in server but I need when click on file download dialogue box comes up and ready for download. How I have to improve my code.

 if (Page.IsPostBack == false)
               {
                 System.IO.DirectoryInfo RootDir = new System.IO.DirectoryInfo(Server.MapPath("~/Document/Detail  Engineering/"));

        // output the directory into a node
        TreeNode RootNode = OutputDirectory(RootDir, null);

        // add the output to the tree
        TreeView1.Nodes.Add(RootNode);
        TreeView1.CollapseAll();
    }
}

TreeNode OutputDirectory(System.IO.DirectoryInfo directory, TreeNode parentNode)
{
    // validate param
    if (directory == null) return null;

    // create a node for this directory
    TreeNode DirNode = new TreeNode(directory.Name);

    // get subdirectories of the current directory
    System.IO.DirectoryInfo[] SubDirectories = directory.GetDirectories();

    // output each subdirectory
    for (int DirectoryCount = 0; DirectoryCount < SubDirectories.Length; DirectoryCount++)
    {
        OutputDirectory(SubDirectories[DirectoryCount], DirNode);
    }

    // output the current directories files
    System.IO.FileInfo[] Files = directory.GetFiles();

  for (int FileCount = 0; FileCount < Files.Length; FileCount++)
   {
       string sFullname = directory.FullName;
       string sStr = sFullname + "\\" + Files[FileCount].Name;
       System.IO.FileInfo file = new System.IO.FileInfo(sStr);
       DirNode.ChildNodes.Add(new TreeNode(Files[FileCount].Name, Files[FileCount].Name));

       TreeNode subnode = new TreeNode(Files[FileCount].Name);
       string test = subnode.ValuePath;
      subnode.NavigateUrl = sStr;
      subnode.SelectAction = TreeNodeSelectAction.Select;




    }

    // if the parent node is null, return this node
    // otherwise add this node to the parent and return the parent
    if (parentNode == null)
    {
        return DirNode;
    }
    else
    {
        parentNode.ChildNodes.Add(DirNode);
        return parentNode;
    }
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
    string filename = Server.MapPath(TreeView1.SelectedValue);
    string test = TreeView1.SelectedNode.ValuePath;
    System.IO.FileInfo file = new System.IO.FileInfo(filename);

    if (file.Exists)
   {
        //System.Diagnostics.Process.Start(file.FullName);

        string filePath = filename;//ur file path on the server
        Response.ContentType = "application/pdf";//"application/ms-word";
        Response.WriteFile(filePath);

    }

`

2
Contributors
1
Reply
5
Views
4 Years
Discussion Span
Last Post by LastMitch
0

I have used below code for structure of folders and files in server but I need when click on file download dialogue box comes up and ready for download.

I got no idea what you are trying. The code you provide is just a path to the directory.

I'm not sure how you(or anyone) are going to create a button to download a file from the directory?

I even google this too and I can't even anything regarding about this too.

Edited by LastMitch: grammer

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.