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?
Related Article: How to gzip a file with javascript?
is a JavaScript / DHTML / AJAX discussion thread by shadiadiph that has 5 replies, was last updated 6 months ago and has been tagged with the keywords: javascript, gzip, file.
FALL3N
Junior Poster in Training
87 posts since May 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0
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
87 posts since May 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0
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
87 posts since May 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0
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
87 posts since May 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0
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
87 posts since May 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0
Question Answered as of 1 Year Ago by
tawes01,
macgurl70,
ivan3510
and 2 others
stbuchok
Practically a Posting Shark
875 posts since May 2011
Reputation Points: 138
Solved Threads: 124
Skill Endorsements: 2