Solved

Opening and viewing a text file

Posted on 2006-11-29
11
486 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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now