如何点击treeviewnode来下载文件
我想在树视图上列出文件,如果我单击树节点(一个文件),将下载该文件:
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的应用程序是 Intranet 应用程序,那么这可能会起作用:
如果它不是 Intranet 应用程序,您可以挂钩树视图上的 selectedNodeChanged 事件,并使用文件流传输器将文件流式传输到客户端。但您很可能必须根据您计划发送的文件来指定文件的 MIME 类型。
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.
我会使用 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.