2011-07-01 29 views
1

我正在製作一個模塊,顯示文件夾中我的驅動器上存儲的文檔的樹視圖。它檢索得很好。但問題是這些文檔的格式不同(.pdf,.docx等)。點擊瀏覽器時無法打開。在那裏顯示404.4錯誤。所以告訴我如何通過點擊按鈕來下載/打開不同的格式文件?以下是我的代碼:如何下載/打開我通過服務器路徑搜索的文件?

protected void Page_Load(System.Object sender, System.EventArgs e) 
    { 
     try 
     { 
      if (!Page.IsPostBack) 
      { 
       if (Settings["DirectoryPath"] != null) 
       { 
        BindDirectory(Settings["DirectoryPath"].ToString()); 
       } 
       else 
       { 
        BindDirectory(Server.MapPath("~/")); 
       } 
      } 
     } 
     catch (DirectoryNotFoundException DNEx) 
     { 
      try 
      { 
       System.IO.Directory.CreateDirectory("XIBDir"); 
       BindDirectory(Server.MapPath("XIBDir")); 
      } 
      catch (AccessViolationException AVEx) 
      { 
       Response.Write("<!--" + AVEx.Message + "-->"); 
      } 

     } 
     catch (Exception exc) //Module failed to load 
     { 
      Exceptions.ProcessModuleLoadException(this, exc); 
     } 

    } 

    #endregion 

    #region Optional Interfaces 

    /// ----------------------------------------------------------------------------- 
    /// <summary> 
    /// Registers the module actions required for interfacing with the portal framework 
    /// </summary> 
    /// <value></value> 
    /// <returns></returns> 
    /// <remarks></remarks> 
    /// <history> 
    /// </history> 
    /// ----------------------------------------------------------------------------- 
    public ModuleActionCollection ModuleActions 
    { 
     get 
     { 
      ModuleActionCollection Actions = new ModuleActionCollection(); 
      Actions.Add(this.GetNextActionID(), Localization.GetString(ModuleActionType.AddContent, this.LocalResourceFile), ModuleActionType.AddContent, "", "", this.EditUrl(), false, SecurityAccessLevel.Edit, true, false); 
      return Actions; 
     } 
    } 

    #endregion 

    private void BindDirectory(string Path) 
    { 
     try 
     { 
      System.IO.DirectoryInfo dirRoot = new System.IO.DirectoryInfo(Path); 

      TreeNode tnRoot = new TreeNode(Path); 
      tvDirectory.Nodes.Add(tnRoot); 

      BindSubDirectory(dirRoot, tnRoot); 
      tvDirectory.CollapseAll(); 
     } 
     catch (UnauthorizedAccessException Ex) 
     { 
      TreeNode tnRoot = new TreeNode("Access Denied"); 
      tvDirectory.Nodes.Add(tnRoot); 
     } 
    } 

    private void BindSubDirectory(System.IO.DirectoryInfo dirParent, TreeNode tnParent) 
    { 
     try 
     { 
      foreach (System.IO.DirectoryInfo dirChild in dirParent.GetDirectories()) 
      { 
       //TreeNode tnChild = new TreeNode(dirChild.Name); 
       TreeNode tnChild = new TreeNode(dirChild.Name, dirChild.FullName); 

       tnParent.ChildNodes.Add(tnChild); 

       BindSubDirectory(dirChild, tnChild); 
      } 
     } 
     catch (UnauthorizedAccessException Ex) 
     { 
      TreeNode tnChild = new TreeNode("Access Denied"); 
      tnParent.ChildNodes.Add(tnChild); 
     } 
    } 


    private void BindFiles(string Path) 
    { 
     try 
     { 
      tvFile.Nodes.Clear(); 
      System.IO.DirectoryInfo dirFile = new System.IO.DirectoryInfo(Path); 

      foreach (System.IO.FileInfo fiFile in dirFile.GetFiles("*.*")) 
      { 
       string strFilePath = Server.MapPath(fiFile.Name); 
       string strFilePaths = "~/" + fiFile.FullName.Substring(15); 

       TreeNode tnFile = new TreeNode(fiFile.Name, fiFile.FullName, "", strFilePaths, "_blank"); 
       tvFile.Nodes.Add(tnFile); 
      } 
     } 
     catch (Exception Ex) 
     { 
      Response.Write("<!--" + Ex.Message + "-->"); 
     } 
    } 
    protected void tvDirectory_SelectedNodeChanged(object sender, EventArgs e) 
    { 
     try 
     { 
      string strFilePath = tvDirectory.SelectedNode.Value; 
      BindFiles(tvDirectory.SelectedNode.Value); 
     } 
     catch (Exception Ex) 
     { 
      Response.Write("<!--" + Ex.Message + "-->"); 
     } 
    } 
} 

}

回答

0

我會在你的TreeView使用超鏈接打開的鏈接:openfile.ashx路徑= [insertpathhere](確保你的鏈接在目標打開= 「_blank」)

您的通用處理程序(ASHX)中有權訪問從磁盤加載文件,並將其字節流傳送到responseStream中。這會導致文件在瀏覽器中下載。您還應該在適用的情況下設置內容類型。

代碼示例請求......

前言:有一些「額外」的東西會在這裏......我Base64編碼在我的例子路徑,因爲我沒有想的路徑,是「人類可讀」 。此外,當我把它關閉瀏覽器,我的預待定「出口爲」加時間戳...但你的想法...

public class getfile : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     var targetId = context.Request.QueryString["target"]; 
     if (string.IsNullOrWhiteSpace(targetId)) 
     { 
      context.Response.ContentType = "text/plain"; 
      context.Response.Write("Fail: Target was expected in querystring."); 
      return; 
     } 
     try 
     { 
      var url = new String(Encoding.UTF8.GetChars(Convert.FromBase64String(targetId))); 
      var filename = url.Substring(url.LastIndexOf('\\') + 1); 
      filename = "export-" + DateTime.Now.ToString("yyyy-MM-dd-HHmm") + filename.Substring(filename.Length - 4); 
      context.Response.ContentType = "application/octet-stream"; 
      context.Response.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", filename)); 
      var data = File.ReadAllBytes(url); 
      File.Delete(url); 
      context.Response.BinaryWrite(data); 
     } 
     catch (Exception ex) 
     { 
      context.Response.Clear(); 
      context.Response.Write("Error occurred: " + ex.Message); 
      context.Response.ContentType = "text/plain"; 
      context.Response.End(); 
     } 
    } 
    public bool IsReusable { get { return false; } } 
} 
+0

請給我發送它的代碼嗎? – Manish

+0

當然,給我一分鐘 –

+0

爲您的項目添加正確的類型...添加>新項目...>通用處理程序 –

0

404.4意味着Web服務器(IIS大概)現在不會如何提供文件(基於擴展名)。如果您的代碼正確地提供其他文件,這是一個Web服務器配置問題。檢查您的服務器文檔,爲不起作用的文件擴展名添加適當的處理程序。

相關問題