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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text file into a textarea?

Status
Not open for further replies.

rxsid

Programmer
Jul 9, 2002
57
0
0
US
Hi all,

I'm a novice asp coder. I've got a web page that have a <textarea> that I need to populate with what is in a text file. For now, I just want to Read the text file into the textarea, but I just can't seem to get it to work. Here's what I've got:
Code:
Function getFile(fileThis)
    dim Filename

    Filename = "someFile.txt"
    'format is data1|data2

    Const ForReading = 1, ForWriting = 2, ForAppending = 3
    Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

    ' Create a filesystem object
    Dim fso
    set fso = server.createObject("Scripting.FileSystemObject")

    ' Map the logical path to the physical system path
    Dim Filepath
    Filepath = Server.MapPath(Filename)

    if fso.FileExists(Filepath) Then
	    ' Get a handle to the file
	    Dim file, Line
	    set file = fso.GetFile(Filepath)

	    ' Open the file
	    Dim TextStream
	    Set TextStream = file.OpenAsTextStream(ForReading, TristateUseDefault)

	    ' Read the file line by line
	    dim counter, UserID(), PW()
            counter = 1


	    Do While Not TextStream.AtEndOfStream
	        Line = TextStream.readline
	        arrayFile = split(Line,"|")
	        data1(counter) = arrayFile(0)
	        data2(counter) = arrayFile(1)
	        counter = counter + 1
	    Loop

	    Set TextStream = nothing

    Else
        Response.Write("<font size=2 color=red>Users file not found on server</font><br>" & Filename)
    End If
End Function
and here is the html
Code:
	    <textarea name="somename" cols="30" rows="15">
	    <% Thisfile = getFile(fileThis) %>
	    </textarea>
and yeah, I would like to leave this as a function if at all possible (due for many reasons, but primarily because that's the style already existing in many many asp pages at our company and no direction to change it).

I've tried response.writting the data1 and data2 out within the do while loop as well as right after calling the function in the textarea by doing a for x = 1 to counter. nothing seems to work (no data displayed in the textarea).


Thanks for any help.
 
Ok, first of all, your function is not returning a value, which is why it doesn't output anything. Additionally your not acvtually writing the contents, your just assigning them to a variable. If your function were returning a value (and a string value, not arrays) then you would want something like:
Code:
<textarea name="somename" cols="30" rows="15">
<% Response.Write getFile(fileThis) %>
</textarea>

or the shorter method:

<textarea name="somename" cols="30" rows="15">
<%=getFile(fileThis) %>
</textarea>

Next, it's generally not good practice to make functions depend on variables declared outside of them. This can make them confusing to re-use, difficult to debug, and generally just a pain.
A better solution is to either return the value (in VBScript this is done by setting th function name = to the value you want to retun) or passing in extra variables to fill with the return values.

Next, I don't see where your Dim'ing your arrays, but I am surprised your not getting out of bounds errors.


My suggestion would be to rewrite the function a little. A better solution would be to return the data in a single array, with each element of the array holding an array of the line contents.

Since you can't Response.Write an array, I would suggest either modifying your funtciotn to return a string, or creating a second function to return a string to use in your textbox. This could be put together fairly easily, like so:
Code:
Function FileToText(filePath)
   Dim fttFso, fttFil
   Set fttFso = Server.CreateObject("Scripting.FileSystemObject")
   If fttFso.FileExists(filePath) Then
      Set fttFil = fttFso.OpenTextFile(filePath,1)
      FileToText = fttFso.ReadAll()
      fttFil.Close
      Set fttFil = Nothing
   Else
      FileToText = ""
   End If

   Set fttFso = Nothing
End Function

Then you could use it to output into your textarea like so:
Code:
<textarea name="somename" cols="30" rows="15">
<%=FileToText(Server.MapPath("MyFile.txt")) %>
</textarea>

If you instead wanted to return your arrays, then that requires a bit more work. Unfortunatly I am on my way to bed or I would write up an example on that also.
I apologize for the multitude of spelling issues, again I am on my way to bed and hadn't planned on responding to any posts.

-T

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top