I want to list files on a treeview and if I click on a treenode (a file), that file will be downloaded:
<asp:TreeView Id="MyTree"
PathSeparator = "|"
OnTreeNodePopulate="PopulateNode"
ExpandDepth="0"
runat="server" ImageSet="XPDirectoryListing" NodeIndent="15">
<SelectedNodeStyle BackColor="#B5B5B5"></SelectedNodeStyle>
<NodeStyle VerticalPadding="2" Font-Names="Tahoma" Font-Size="8pt" HorizontalPadding="2" ForeColor="#000000"></NodeStyle>
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA"></HoverNodeStyle>
<Nodes>
<asp:TreeNode Text="Demos" PopulateOnDemand="True" Value="Demos" />
</Nodes>
</asp:TreeView>
And code-behind:
public partial class DirectoryListing : System.Web.UI.Page
{
protected void PopulateNode(Object source, TreeNodeEventArgs e)
{
TreeNode node = e.Node;
if (e.Node.Value == "Demos")
{
e.Node.Value = Server.MapPath("~/");
}
String[] dirs = Directory.GetDirectories(node.Value);
// Enumerate directories
foreach (String dir in dirs)
{
TreeNode newNode = new TreeNode(Path.GetFileName(dir), dir);
if (Directory.GetFiles(dir).Length > 0 || Directory.GetDirectories(dir).Length > 0)
{
newNode.PopulateOnDemand = true;
}
node.ChildNodes.Add(newNode);
}
// Enumerate files
String[] files = Directory.GetFiles(node.Value);
foreach (String file in files)
{
TreeNode newNode = new TreeNode(Path.GetFileName(file), file);
node.ChildNodes.Add(newNode);
}
}
}
How can I change it such a way that I click on a treenode, the file at the treenode will be downloaded.
Thanks in advance.
If your application is an intranet app, then this might work:
If it’s not an intranet app you could hook on to the selectedNodeChanged event on the treeview and use a file streamer to stream the file to the client. But you most likely have to specify the MIME type of the file depending on what files you plan to send.