How to Read Text file in ASP

Discussion in 'ASP General' started by sincethe2004, Feb 5, 2004.

  1. sincethe2004

    sincethe2004 Guest

    Hi, I'm write an asp to include part of a text file into my asp output html,
    I use FileSystemObject to get the content of the text file,
    but the asp just hang when it runs,
    the source code is listed below if anyone could help:
    --------------------
    <%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
    <%
    if (String(Request.QueryString("page")) == "undefined" ||
    String(Request.QueryString("page")) == "")
    {
    Response.Write("Please specify the page data.");
    Response.End()
    }

    var sFile = Server.MapPath(String(Request.QueryString("page")));
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    if (!fso.FileExists(sFile)) {
    Response.Write("No Such File : " + String(Request.QueryString("page")));
    //Response.Redirect(".");
    Response.End();
    }

    var f = fso_OpenTextFile(sFile, 1); //1 -- ForReading
    var sFileTxt = f.ReadAll();
    f.Close();

    %>


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1">
    </head>
    <body>
    <%= sFileTxt %>
    </body>
    </html>
    --------------------
    sincethe2004, Feb 5, 2004
    #1
    1. Advertising

  2. sincethe2004

    Steven Burn Guest

    <%
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim FileObject, SomeFile, InputStream, PrintLine, LineCount
    Set FileObject = Server.CreateObject("Scripting.FileSystemObject")
    SomeFile = Server.MapPath("myfile.ext")
    Set InputStream = FileObject.OpenTextFile(SomeFile, ForReading, False)
    LineCount = 0
    Do While Not InputStream.AtEndOfStream
    LineCount = LineCount + 1
    PrintLine = InputStream.ReadLine() + "<br>" + PrintLine
    'do something with the contents......
    Loop
    InputStream.Close
    Set OutputStream = Nothing
    Set FileObject = Nothing
    %>

    --
    Regards

    Steven Burn
    Ur I.T. Mate Group
    www.it-mate.co.uk

    Keeping it FREE!

    Disclaimer:
    I know I'm probably wrong, I just like taking part ;o)


    sincethe2004 <> wrote in message
    news:...
    > Hi, I'm write an asp to include part of a text file into my asp output

    html,
    > I use FileSystemObject to get the content of the text file,
    > but the asp just hang when it runs,
    > the source code is listed below if anyone could help:
    > --------------------
    > <%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
    > <%
    > if (String(Request.QueryString("page")) == "undefined" ||
    > String(Request.QueryString("page")) == "")
    > {
    > Response.Write("Please specify the page data.");
    > Response.End()
    > }
    >
    > var sFile = Server.MapPath(String(Request.QueryString("page")));
    > var fso = new ActiveXObject("Scripting.FileSystemObject");
    > if (!fso.FileExists(sFile)) {
    > Response.Write("No Such File : " + String(Request.QueryString("page")));
    > //Response.Redirect(".");
    > Response.End();
    > }
    >
    > var f = fso_OpenTextFile(sFile, 1); //1 -- ForReading
    > var sFileTxt = f.ReadAll();
    > f.Close();
    >
    > %>
    >
    >
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    > "http://www.w3.org/TR/html4/loose.dtd">
    > <html>
    > <head>
    > <meta http-equiv="Content-Type" content="text/html;
    > charset=iso-8859-1">
    > </head>
    > <body>
    > <%= sFileTxt %>
    > </body>
    > </html>
    > --------------------
    >
    >
    >
    >
    Steven Burn, Feb 5, 2004
    #2
    1. Advertising

  3. sincethe2004

    sincethe2004 Guest

    thanks for your help.

    "Steven Burn" <nobody@PVT_it-mate.co.uk> ¼¶¼g©ó¶l¥ó·s»D
    :elatYn#...
    > <%
    > Const ForReading = 1, ForWriting = 2, ForAppending = 8
    > Dim FileObject, SomeFile, InputStream, PrintLine, LineCount
    > Set FileObject = Server.CreateObject("Scripting.FileSystemObject")
    > SomeFile = Server.MapPath("myfile.ext")
    > Set InputStream = FileObject.OpenTextFile(SomeFile, ForReading, False)
    > LineCount = 0
    > Do While Not InputStream.AtEndOfStream
    > LineCount = LineCount + 1
    > PrintLine = InputStream.ReadLine() + "<br>" + PrintLine
    > 'do something with the contents......
    > Loop
    > InputStream.Close
    > Set OutputStream = Nothing
    > Set FileObject = Nothing
    > %>
    >
    > --
    > Regards
    >
    > Steven Burn
    > Ur I.T. Mate Group
    > www.it-mate.co.uk
    >
    > Keeping it FREE!
    >
    > Disclaimer:
    > I know I'm probably wrong, I just like taking part ;o)
    >
    >
    > sincethe2004 <> wrote in message
    > news:...
    > > Hi, I'm write an asp to include part of a text file into my asp output

    > html,
    > > I use FileSystemObject to get the content of the text file,
    > > but the asp just hang when it runs,
    > > the source code is listed below if anyone could help:
    > > --------------------
    > > <%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
    > > <%
    > > if (String(Request.QueryString("page")) == "undefined" ||
    > > String(Request.QueryString("page")) == "")
    > > {
    > > Response.Write("Please specify the page data.");
    > > Response.End()
    > > }
    > >
    > > var sFile = Server.MapPath(String(Request.QueryString("page")));
    > > var fso = new ActiveXObject("Scripting.FileSystemObject");
    > > if (!fso.FileExists(sFile)) {
    > > Response.Write("No Such File : " +

    String(Request.QueryString("page")));
    > > //Response.Redirect(".");
    > > Response.End();
    > > }
    > >
    > > var f = fso_OpenTextFile(sFile, 1); //1 -- ForReading
    > > var sFileTxt = f.ReadAll();
    > > f.Close();
    > >
    > > %>
    > >
    > >
    > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    > > "http://www.w3.org/TR/html4/loose.dtd">
    > > <html>
    > > <head>
    > > <meta http-equiv="Content-Type" content="text/html;
    > > charset=iso-8859-1">
    > > </head>
    > > <body>
    > > <%= sFileTxt %>
    > > </body>
    > > </html>
    > > --------------------
    > >
    > >
    > >
    > >

    >
    >
    sincethe2004, Feb 6, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krish
    Replies:
    1
    Views:
    1,077
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Oct 20, 2005
  2. Sreejith K
    Replies:
    24
    Views:
    1,015
    Sreejith K
    Mar 24, 2009
  3. Mmcolli00 Mom
    Replies:
    2
    Views:
    194
    Mmcolli00 Mom
    Jan 27, 2009
  4. Alex Dowad
    Replies:
    4
    Views:
    270
    Michel Demazure
    May 1, 2010
  5. Replies:
    19
    Views:
    238
    Andreas Perstinger
    Oct 7, 2013
Loading...

Share This Page