Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Opening and viewing a text file

Posted on 2006-11-29
11
Medium Priority
?
494 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

916 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