Home:ALL Converter>Open a text file in new tab

Open a text file in new tab

Ask Time:2016-06-23T19:05:22         Author:Dheeraj Patnaik

Json Formatter

I have a text file in my local drive. I want to show the content of it in a new tab.

I have tried the following

1st Try

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

2nd Try

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

Both of these open in new tab but the data inside the file is not displaying

After some search, I found this

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

This is displaying the content in my file but along with that it is also displaying my aspx page

So my question is how to show a text file in a new tab

Author:Dheeraj Patnaik,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/37989815/open-a-text-file-in-new-tab
Tom Söhne :

try those steps\n\n\ncreate a log.aspx and put the code from the third snippet to it.\nuse 1st or 2nd snippet to open log.aspx in the new window (tab)\n\n\nthat way it should work.",
2016-06-23T11:11:31
yy