Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to populate an ASP page w/content from a .doc or txt ? 1

Status
Not open for further replies.
Jul 13, 2001
180
0
0
US
Is it possible to populate an ASP page w/content from a .doc or txt file ? If so how? Thank you in advance. : )
 
To do a text file is easy ... just use the FileSystemObject to open a file and read it.

here's one way to do it

<%
option explicit

SUB ReadDisplayFile(FileToRead)
dim whichfile, fs, thisfile, tempSTR

whichfile=server.mappath(FileToRead)
Set fs = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set thisfile = fs.OpenTextFile(whichfile, 1, False)
tempSTR=thisfile.readall
response.write tempSTR
thisfile.Close
set thisfile=nothing
set fs=nothing
END SUB
%>

-Christian
 
Hello Christian,

Thank you for the reply.
I followed through as you instructed, but I receive an empty page with no text.
What am I doing wrong? How should I place the txt file in the parenthesis?

SUB ReadDisplayFile(content.txt)?

Should I place the txt file in the root folder?

Thanks for tolerating a somewhat newbie.
 
338819

Not too sure about this one, also a newbie, but I think it would be something like:

<%
'option explicit '(whatta pain)

ReadDisplayFile(&quot;content.txt&quot;)
'This is the bit you need to change, not the subroutine that Christian wrote-leave that nice bit of code alone...



'=============subroutines==================
SUB ReadDisplayFile(FileToRead)
dim whichfile, fs, thisfile, tempSTR
whichfile=server.mappath(FileToRead)
Set fs = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set thisfile = fs.OpenTextFile(whichfile, 1, False)
tempSTR=thisfile.readall
response.write tempSTR
thisfile.Close
set thisfile=nothing
set fs=nothing
END SUB
'==========================================
%>
 
Oi! Thanks mercutiostwin. But somehow you've managed to confuse me further.

RE: >>>>>>

&quot;<%
'option explicit '(whatta pain)

ReadDisplayFile(&quot;content.txt&quot;)
'This is the bit you need to change, not the subroutine that Christian wrote-leave that nice bit of code alone...
&quot;

????

Can anyone please help??
 
ok ok sorry.

Right: What Christian gave you was a subroutine, or a function that doesn't 'return' anything. It just does something, in this case it writes out the file to the screen, and tidies up after itself.

Why did he use a subroutine? Because he might want to do it more than once, with different files. So using this method all he has to do is say

<%
ReadDisplayFile(&quot;content.txt&quot;)
%>
(Note the inverted commas) and it will go to the subroutine and do the code, looking at what's in the brackets. You leave the subroutine code alone. All you have to worry about is having the subroutine code in there, and writing the code to call the subroutine.

ie what I wrote above.
I have no idea what the path to the file is to be honest but it looks like christian's code takes care of it. I would put the file in the same folder as your script and hope for the best. I always used to have to put absolute paths to the document: i think someone else would have to help you with that if it causes trouble.

so in summary:

<% @language=&quot;vbscript&quot; %>
<%
ReadDisplayFIle(&quot;path-to-document&quot;)


SUB ReadDisplayFile(FileToRead)
dim whichfile, fs, thisfile, tempSTR
whichfile=server.mappath(FileToRead)
Set fs = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set thisfile = fs.OpenTextFile(whichfile, 1, False)
tempSTR=thisfile.readall
response.write tempSTR
thisfile.Close
set thisfile=nothing
set fs=nothing
END SUB
%>
338819
 
Hello Mercutiostwin,

My humble thanks! I shall try it out tomorrow at work.

Thank you so very much! :)

MustangManny
 
IF anyone is interested, this is what this code actually does...


dim whichfile, fs, thisfile, tempSTR
'declares the variables

whichfile=server.mappath(FileToRead)
'sets whichfile to the absolute path of FiletoRead, eg c:\webroot\users\user1\textfile.txt

Set fs = CreateObject(&quot;Scripting.FileSystemObject&quot;)
'sets fs to an instance of the file system object

Set thisfile = fs.OpenTextFile(whichfile, 1, False)
'Opens the textfile, and sets thisfile to that file object

tempSTR=thisfile.readall
'sets tempSTR to the contents of thisfile

response.write tempSTR
'writes tempSTR to the HTML

thisfile.Close
'closes thisfile, the file object
set thisfile=nothing
'sets thisfile to nothing, freeing its resources on the server

set fs=nothing
'sets fs to nothing, freeing its resources on the server

Hope this helps someone.

G
-GTM Solutions, Home of USITE-
-=
 
If u want to include an existing text into an asp page...
This how i did that...


MyAspPage.asp
<%
Function ReadDisplayFile(FileToRead)
dim whichfile, fs, thisfile, tempSTR
whichfile=server.mappath(FileToRead)
Set fs = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set thisfile = fs.OpenTextFile(whichfile, 1, False)
tempSTR=thisfile.readall
thisfile.Close
set thisfile=nothing
set fs=nothing
ReadDisplayFile=tempSTR
end function
%>
...
response.write ReadDisplayFile(&quot;MyPage.txt&quot;)
...


MyPage.txt
put your text to include here


Hope this help...
________

George
 
THANK YOU TO ALL:
SauerC, mercutiostwin , geee, amd shaddow!!
You all made my day.
FYI, I also tried using htm as well as txt and it works great!!! : )

Forgive the tardy reply.

THANKS AGAIN!!! : )

Manny
 
SauerC, mercutiostwin , geee, or shaddow, perhaps you can help me on this one.
All works real well as mentioned above BUT.
Can I place a txt file from a Virtual Directory?

I.e.

Previous: response.write ReadDisplayFile(&quot;MyPage.txt&quot;)

What I would like to do:
response.write ReadDisplayFile(&quot;virtual directory path&quot;)

FYI: I have a virtual directory created in IIS4 where it is linked to another server. Through the browser I would use the url to view that file.
So in essence I wanted to use the above method of including the text file as you all showed me. But what I wanted to do is have my co-worker drop his files into the virtual directory which then would be included in my pages through the above vbscript.

Again, your assistance is greatly appreciated.

Thanks in advance,

Manny
 
You can also use an ODBC connection to the text file if that suits your needs
 
Thanks Winter, however, having my coworker drag and drop his txt forms would make it easier and would not have to require inputting,updating a database and omit them from having to learn basic html by just usung a txt file where I can have the asp page dictate the font, look and feel.

Anyone know if this is possible?

response.write ReadDisplayFile(&quot;[virtual directory path created in IIS]&quot;)

FYI: I have a virtual directory created in IIS4 where it is linked to another server. Through the browser I would use the url to view that file.
So in essence I wanted to use the above method of including the text file as you all showed me. But what I wanted to do is have my co-worker drop his files into the virtual directory which then would be included in my pages through the above vbscript
 
Im' not too sure about the server admin side of things, but shouldn't it be easy enough to just reference the actual path because the virtual directory maps to an existing real directory on the server anyway?
 
Hello mercutiostwin ,

Actually the virtual directory is pointed to a seperate server on the network.
Would it be feasible? How should the path be written?

Thanks again,

Manny
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top