2016-06-23 53 views
0

我的本地驅動器中有一個text文件。我想在新標籤中顯示它的內容。在新選項卡中打開文本文件

我曾嘗試以下

1日試

string fileName = @"C:\MyFile.log"; 
    Page.ClientScript.RegisterStartupScript(GetType(), "windowKey", "window.open('" + fileName + "');", true); 

第二個嘗試

Response.Write("<script>"); 
    Response.Write("window.open('C:\\MyFile.log', '_newtab');"); 
    Response.Write("</script>"); 

這兩個在新標籤中打開,但裏面的數據文件是不顯示

一些搜索之後,我發現這個

FileStream MyFileStream = new FileStream(@"C:\MyFile.log", FileMode.Open); 
    long FileSize; 
    FileSize = MyFileStream.Length; 
    byte[] Buffer = new byte[(int)FileSize]; 
    MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length); 
    MyFileStream.Close(); 
    Response.ContentType = "text/plain"; 
    Response.AddHeader("content-disposition", "inline; filename=sample.txt"); 
    Response.BinaryWrite(Buffer); 

這顯示在我的文件,但沿含量,它也顯示我aspx

所以我的問題是,如何在新選項卡中顯示文本文件

+0

@madalinivascu沒關係,你能告訴我如何指定我的路徑 –

+0

您使用A HREF鏈接像你鏈接網頁到另一個笑 – madalinivascu

+0

@madalinivascu我已經試過這''link並得到了錯誤的' console'作爲**不允許加載本地資源** –

回答

0

嘗試那些步驟

  • 創建一個log.aspx並將代碼從第三個片段放到它。
  • 使用第一或第二片段在新窗口中打開log.aspx(標籤)

這樣,它應該工作。

+0

我不想爲此創建另一個'aspx' –

0

我已經試過這樣:

Default.aspx的

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test.Default" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:ScriptManager runat="server"></asp:ScriptManager> 
     <asp:Button id="btnShowContent" Text="Show me the text" runat="server" OnClick="btnShowContent_Click" /> 
    </div> 
    </form> 
</body> 
</html> 

Default.aspx.cs

protected void btnShowContent_Click(object sender, EventArgs e) 
{ 
    ScriptManager.RegisterStartupScript(this, typeof(string), "OpenTextFile", "Javascript:window.open('Resources/test.txt');", true); 
} 

你只需要更新文件的路徑。 希望這是你想要的。