如何点击treeviewnode来下载文件

发布于 2025-01-05 01:14:23 字数 1908 浏览 5 评论 0原文

我想在树视图上列出文件,如果我单击树节点(一个文件),将下载该文件:

<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>

并且隐藏代码:

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

如何更改它以单击树节点(文件位于将下载树节点。

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何处潇湘 2025-01-12 01:14:23

如果您的应用程序是 Intranet 应用程序,那么这可能会起作用:

TreeNode newNode = new TreeNode(Path.GetFileName(file), file);
            newNode.SelectAction = TreeNodeSelectAction.Select;
            newNode.NavigateUrl = Path.GetFullPath(file);
            node.ChildNodes.Add(newNode);

如果它不是 Intranet 应用程序,您可以挂钩树视图上的 selectedNodeChanged 事件,并使用文件流传输器将文件流式传输到客户端。但您很可能必须根据您计划发送的文件来指定文件的 MIME 类型。

If your application is an intranet app, then this might work:

TreeNode newNode = new TreeNode(Path.GetFileName(file), file);
            newNode.SelectAction = TreeNodeSelectAction.Select;
            newNode.NavigateUrl = Path.GetFullPath(file);
            node.ChildNodes.Add(newNode);

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.

沧桑㈠ 2025-01-12 01:14:23

我会使用 jquery 将树节点转换为链接。一旦您获得了链接,就会捕获 onclick javascript 事件,并在该事件中将 iframe 添加到具有文件地址的页面。这将导致文件被下载。

I would use jquery to turn the tree nodes into links. Once you have the link capture the onclick javascript event and in that event add an iframe to the page that has the address of the file. This will cause the file to be downloaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文