如何在浏览器中从Treeview节点(pdf,docx,xl​​s)打开文件 [英] how to open files from treeview node (pdf,docx,xls) in the browser

查看:113
本文介绍了如何在浏览器中从Treeview节点(pdf,docx,xl​​s)打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

如何在浏览器中从Treeview节点(pdf,docx,xl​​s)打开文件.


我想通过单击节点打开特定文件.

我使用以下代码填充了树形视图

Hi all,

How to open files from treeview node (pdf,docx,xls) in the browser.


i want to open the specific files by clicking on the node.

I used the following code to populate the tree-view

protected void Page_Load(object sender, EventArgs e)
   {
      TreeView1.Nodes[0].Value = Server.MapPath("FORMS_&_FORMATS");

   }
   protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
   {
       if (IsCallback == true)
       {
           if (e.Node.ChildNodes.Count == 0)
           {
               LoadChildNode(e.Node);
           }
       }
   }
   private void LoadChildNode(TreeNode node)
   {
       DirectoryInfo directory = null;
       directory = new DirectoryInfo(node.Value);

       foreach (DirectoryInfo subtree in directory.GetDirectories())
       {
           TreeNode subNode = new TreeNode(subtree.Name);
           subNode.Value = subtree.FullName;
           try
           {
               if (subtree.GetDirectories().Length > 0 | subtree.GetFiles().Length > 0)
               {
                   subNode.SelectAction = TreeNodeSelectAction.SelectExpand;
                   subNode.PopulateOnDemand = true;
                   subNode.NavigateUrl = "#";
               }
           }
           catch
           {

           }
           node.ChildNodes.Add(subNode);
       }
       foreach (FileInfo fi in directory.GetFiles())
       {
           TreeNode subNode = new TreeNode(fi.Name);
           node.ChildNodes.Add(subNode);
          //subNode.NavigateUrl = "FORMS_&_FORMATS/" + fi.Name.ToString();
           subNode.NavigateUrl = Server.MapPath(fi.Name.ToString());
       }
   }






请帮帮我.

预先感谢






Please help me.

Thanks in advance

推荐答案

做到这一点的一般方法是导航到一个特殊的页面,该页面仅用于返回适当的资源.在此页面中,您需要做一些事情,因此,我将在这里尝试对其进行总结.

您将要使用响应对象,因此您可能想使用它.我倾向于使用看起来像这样的辅助方法:
The general way to do this is to navigate to a special page that simply serves to return the appropriate resource. There are a few things that you need to do in this page, so I will attempt to summarise this here.

You are going to be working with the response object, so you would probably want to use that. I tend to use a helper method that looks something like this:
public void DisplayItem(string file, string mimeType, byte[] fileData)
{
  HttpResponse response = HttpContext.Current.HttpResponse;
  response.Buffer = true;
  response.Clear();
  response.ContentType = mimeType;
  // If I want to save the file to disk with a default filename, I would use:
  //response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
  response.BinaryWrite(fileData);
  response.Flush();
  response.End();
}

对,我们在这里得到了什么?好吧,首先我们设置响应对象并将其清除.然后,我们告诉响应对象它将提供什么内容类型(例如,一个PDF是application/pdf).我通常会在某处使用一个带有文件扩展名的映射,并将其映射到适当的内容类型.

根据是否要将文件保存到磁盘而不是在浏览器中打开文件,下一步是可选的.我们向响应对象添加一个标头,告诉它内容的内容是什么.这个特殊的标头是MIME协议的扩展,它告诉浏览器如何显示文件.

最后,我们写入数据,刷新数据并结束响应.然后将生成的文件发送到浏览器,但不能保证它会显示该文件,因为用户可能没有适当的应用程序来服务MIME类型.在这种情况下,将提示用户将文件保存到磁盘.

Right, what have we got in here? Well, first of all we set up the response object and clear it down. Then we tell the response object what content type it is going to serve up (a PDF, for instance, is application/pdf). I would normally have a mapping somewhere which takes the filename extension and maps it to the appropriate content type.

The next step is optional based on whether or note we want to save the file to disk rather than opening it in the browser. We add a header to the response object telling it what the content-disposition is. This special header is an extension to the MIME protocol that tells the browser how to display the file.

Finally, we write the data, flush it and end the response. The resulting file will then be sent to the browser, but there is no guarantee that it will display the file because the user might not have the appropriate application in place to service the MIME type. In this case, the user will be prompted to save the file to disk.


这篇关于如何在浏览器中从Treeview节点(pdf,docx,xl​​s)打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆