Solved

Opening and viewing a text file

Posted on 2006-11-29
11
492 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
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/…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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