Solved

Opening and viewing a text file

Posted on 2006-11-29
11
488 Views
Last Modified: 2010-05-18
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
Comment
Question by:cjohnson300
  • 6
  • 5
11 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 18036998
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
 

Author Comment

by:cjohnson300
ID: 18037014
Ok, but can I open the file in the application it was intended for ie. notepad?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 18037108
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:cjohnson300
ID: 18037223
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 18037258
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
 

Author Comment

by:cjohnson300
ID: 18037303
Sorry, but I still have the same error
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 18037315
Can you post the code as you currently have it ?
0
 

Author Comment

by:cjohnson300
ID: 18037359
<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
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 18038099
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
 

Author Comment

by:cjohnson300
ID: 18038224
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 18038300
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question