Opening a txt file (notepad file ) adding page source code to it

Last post 07-18-2009 1:16 AM by harminder singh. 2 replies.

Sort Posts:

  • Opening a txt file (notepad file ) adding page source code to it

    07-18-2009, 12:39 AM

    Hi,
    I am Opening a simple  txt file in Asp.net.It is opening the file but at the end of the file
    It is adding all the source code of my page.

    Here's the code                    


                                TextBox1.Text = CommonFunction.ToString(Session["FilePath"]);
                                string lfilepath = TextBox1.Text;
                                string pat = @"\\(?:.+)\\(.+)\.(.+)";
                                Regex r = new Regex(pat);
                                //run
                                Match m = r.Match(lfilepath);
                                string lfile_ext = m.Groups[2].Captures[0].ToString();
                                string lfilename = m.Groups[1].Captures[0].ToString();
                                string lfile = lfilename + "." + lfile_ext;

                                //string path = Server.MapPath((".\\UploadedFile\\1\\ABCDSummary.txt"));
                                string path = CommonFunction.ToString(Session["FilePath"]);

                                FileStream fs = new FileStream(path, FileMode.Open);
                                //fs = File.Open(path, FileMode.Open);
                                byte[] bytBytes = new byte[fs.Length + 1];
                                fs.Read(bytBytes, 0, (int)fs.Length);
                                fs.Close();                        
                                Response.AddHeader("Content-Disposition", "attachment; filename=" + lfile);
                                Response.ContentType = "APPLICATION/OCTET-STREAM";
                                Response.Flush();
                                Response.BinaryWrite(bytBytes);
                               

  • Re: Opening a txt file (notepad file ) adding page source code to it

    07-18-2009, 1:03 AM
    Answer
    • Member
      498 point Member
    • @ndreas
    • Member since 07-16-2009, 1:09 PM
    • Posts 95

    after fs.Close()

    ..

    Response.Clear();
    Response.AddHeader("Content-Disposition", "attachment; filename=" + lfile);
    Response.ContentType = "APPLICATION/OCTET-STREAM";
    Response.BinaryWrite(bytBytes);
    Response.Flush();
    Response.End();

  • Re: Opening a txt file (notepad file ) adding page source code to it

    07-18-2009, 1:16 AM

    Thanks......

Page 1 of 1 (3 items)