all groups > inetserver asp components > february 2004 >
You're in the

inetserver asp components

group:

Read in and display a text file?


Re: Read in and display a text file? Ray at <%=sLocation%> [MVP]
2/25/2004 8:06:27 AM
inetserver asp components:
<!-- #include file="file.txt" -->

Ray at home


[quoted text, click to view]

Re: Read in and display a text file? Ray at <%=sLocation%> [MVP]
2/25/2004 9:27:55 AM
Instead of including the file, you can server.Execute it (IIS5+)

<%
Dim MyFilename
MyFilename = Request.Form("Filename")
%>

<table>
<tr>
<td><%=MyFilename%></td>
<td><% Server.Execite MyFilename %></td>
</tr>
</table>

But, something that will give you a bit more flexibility would be to use the
File System Object.
(http://msdn.microsoft.com/library/en-us/script56/html/FSOoriFileSystemObjec
t.asp)

You can do something like:


<object runat="server" progid="Scripting.FileSystemObject"
id="oFSO"></object>

<%
MyFilename = Request.Form("MyFilename")
sFilepath = Server.MapPath(MyFilename)
If MyFilename <> "" Then
If oFSO.FileExists(sFilepath) Then
sContents = Server.HTMLEncode(oFSO.OpenTextFile(sFilepath, 1).ReadAll)
RESPONSE.WRITE SCONTENTS
End If
Else
sContents = "(File does not exist.)"
End If
%>

<table>
<tr>
<td><%=MyFilename%></td>
<td><%=sContents%></td>
</tr>
</table>



Ray at work






[quoted text, click to view]

Re: Read in and display a text file? Ray at <%=sLocation%> [MVP]
2/25/2004 9:30:48 AM
The SHOUTED line shouldn't be there. I forgot to delete it. Sorry about
that.

Ray at work

"Ray at <%=sLocation%> [MVP]" <myfirstname at lane34 dot com> wrote in
message news:OVaHQu6%23DHA.2636@TK2MSFTNGP09.phx.gbl...

[quoted text, click to view]

Read in and display a text file? owen
2/25/2004 10:38:40 AM

Hi,

How can I read a .txt file and display it on my .asp page?
Looking for a simple example please..!

Thanks
Owen

Re: Read in and display a text file? owen
2/25/2004 1:18:21 PM

"Ray at <%=sLocation%> [MVP]" <Too many private support requests - Ask for
[quoted text, click to view]

But what if the filename isn't hardcoded. Eg. it comes from a database or
is typed on the web page. (Or anything besides hardcoded in an #include
statement!).

My situation is that I have a database table of .txt filenames and
descriptions. The filenames correspond to files on the webserver. I have
no problem getting my info from the database but how can I *dynamically*
insert the content of a given filename on the page.

For example,

<%
Dim MyFilename
MyFilename=Request.Form("Filename")
%>

<table>
<tr>
<td><%=filename%></td>
<td>
<<< read and display 'MyFilename' content here >>>
</td>
</tr>
</table>


See what I mean? The filename is chosen interactively and therefore an
#include statement wont work.


Owen


Re: Read in and display a text file? owen
2/25/2004 3:50:59 PM

"Ray at <%=sLocation%> [MVP]" <myfirstname at lane34 dot com> wrote in
message news:#q#w3v6#DHA.2520@TK2MSFTNGP11.phx.gbl...
[quoted text, click to view]

Err... no problem, Ray.
Thanks for the help - exactly what I needed. :o)

Owen


AddThis Social Bookmark Button