• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 495
  • Last Modified:

Opening and viewing a text file

What is the best (and easiest) way to open a text file using an ASP page so that it is displayed on the client's machine?  

Should the text file be created on the client or the server?

Many thanks
0
cjohnson300
Asked:
cjohnson300
  • 6
  • 5
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
You should create your file on the server as, the majority of the time, you won't have permissions to create the file on the client.

In order to open/read the file you would use the FileSystemObject:

    <%
        Set fso = Server.CreateObject("Scripting.FileSystemObject")
        Set ts = fso.OpenTextFile("Path_and_Name_of_File")

        myVal = ts.ReadAll
        ts.Close

        Response.Write myVal
    %>
0
 
cjohnson300Author Commented:
Ok, but can I open the file in the application it was intended for ie. notepad?
0
 
Carl TawnSystems and Integration DeveloperCommented:
Try something like:

<%
  Response.AddHeader "content-disposition","attachment;filename=Your_Filename.txt"
  Response.ContentType = "application/NOTEPAD"

  Set fso = Server.CreateObject("Scripting.FileSystemObject")
  Set ts = fso.OpenTextFile("Your_Path_and_Filename.txt")

  Response.Write ts.ReadAll
  ts.Close
%>
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
cjohnson300Author Commented:
That causes an error saying that the site requested is unavailable.  What structure should I use for the path in the OpenTextFile option, should it be c:\.... or http://.....
0
 
Carl TawnSystems and Integration DeveloperCommented:
Ideally you should store your files in a folder or a subfolder of your website and then just use a relative path:

    Set ts = OpenTextFile( Server.MapPath("/myfiles/somefile.txt") )
0
 
cjohnson300Author Commented:
Sorry, but I still have the same error
0
 
Carl TawnSystems and Integration DeveloperCommented:
Can you post the code as you currently have it ?
0
 
cjohnson300Author Commented:
<html>
<body>
<%
  Response.AddHeader "content-disposition","attachment;filename=tmp.txt"
  Response.ContentType = "application/NOTEPAD"

  Set fso = Server.CreateObject("Scripting.FileSystemObject")
    Set ts = OpenTextFile(Server.MapPath("tmp.txt") )
  Response.Write ts.ReadAll
  ts.Close
%>
</body>
</html>

I have not put a path in because the text file and the asp page are in the same directory currently

Many thanks
0
 
Carl TawnSystems and Integration DeveloperCommented:
You have a typing error in your code, but other than that it should work ok.

The line:

    Set ts = OpenTextFile(Server.MapPath("tmp.txt") )

Should say:

    Set ts = fso.OpenTextFile(Server.MapPath("tmp.txt") )

You should also take out the HTML at the start and end, otherwise that will also get displayed in notepad.
0
 
cjohnson300Author Commented:
That's great, many thanks!  Don't suppose I can get rid of that message about File Download security warning?  Is that a setting in the browser?

Thanks again
0
 
Carl TawnSystems and Integration DeveloperCommented:
Its a security feature of the browser. It basically treats it as a file download, so prompts the user first to allow it to open.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now