javascript read a text file
I could probably/def read a text file with javascript by embedding some ruby in there but I want to know how to do it with javascript. My code follows:
<HTML>
<HEAD>
<TITLE>"Text File"</TITLE>
<SCRIPT LANGUAGE = JAVASCRIPT>
function handleFile() {
var myFile = document.getElementById('txtF')
var fileContents = System.IO.File.ReadAllLines(myFile);
document.form1.textfield.value=fileContents;
}
</SCRIPT>
</HEAD>
<BODY>
<input type="file" id="aFile" name="txtF"/>
<form name="form1">
<input type="text" name="textfield" value="">
</form>
<input type="button" value="Read File" onclick="handleFile();">
<!-- <button onclick="loadTxtDoc('textfile.txt')">Click</button> //-->
</BODY>
</HTML>
what am I doing wrong?
FALL3N
Junior Poster in Training
84 posts since May 2010
Reputation Points: 10
Solved Threads: 2
oh ok, I didn't realize javascript was not a simple language to use to read text files... like in regular Java, it is SO easy to read a text file. what is this other way to read xml files then?
FALL3N
Junior Poster in Training
84 posts since May 2010
Reputation Points: 10
Solved Threads: 2
thanks! Yeah, I'll def check out YQL and Yahoo Pipes. And, thanks for that link about XML, I'll try that too.. I'll probably end up usingi a combo of these answers..
oh and Tawes, what do you mean that js can reference files, but not read them? cuz that might be enough..
FALL3N
Junior Poster in Training
84 posts since May 2010
Reputation Points: 10
Solved Threads: 2
Thanks so Much! that is exactly what I am trying to do! I am trying to make a list on my webpage based on a CSV file I get updated versions of every so often...
Only problem is, the 'FileSystemObjects' from Tawes do not seem to work properly on my Linux Ubuntu machine.
And Macgurl70, that code is great! Looks like it does exactly what I need the code to do... but the table (that is generated after pushing the button) is blank.. and I read the text file, it is DEFINITELY not blank... does this also not work on linux?
FALL3N
Junior Poster in Training
84 posts since May 2010
Reputation Points: 10
Solved Threads: 2
oh... wow, nvm.. I am very dumb :D all I had to do was change the name of the list file from, 'TestingOne.txt' to 'TestingOne.csv'...
Wow, sorry about that! I am kinda special I guess :\
Thanks again for the code!
FALL3N
Junior Poster in Training
84 posts since May 2010
Reputation Points: 10
Solved Threads: 2