javascript read a text file

 
0
 

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?

 
0
 

I don't think you can read the file with javascript (but nevermind).
Your mistake:

var myFile = document.getElementById('txtF')
//this means: ID="txtF", and you don't have it (you have name="txtF")
//so, you have to replace it (name) with ID (or add id='txtF')
 
0
 

use jquery:

$(".content").load("file.html");
 
0
 

It's much easier to read an XML file, but if you really want to read a text file, I've done it before. I used this tutorial/guide, and it was very helpful.

 
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?

 
0
 

I'm not sure exactly how, but I know it's easier. It's still kind of complicated, though.
http://www.w3schools.com/xml/xml_parser.asp

Now, you should know that Javascript is not a file-manipulating language. In other words, it is not meant for, and is not really capable of (at least alone, without activex) reading external files. It can reference them, but not really access the content of those files and store it in a variable.

 
0
 

Yahoo Pipes has the ability to read the contents of file and assign them to a variable that can be invoked from client side javascript.

It's close cousin YQL also has 'storage pockets' 100kb in size that the contents of which can be manipulated by browser javascript in conjuntion with Yahoo Pipes.

However doing it this way is an ugly hack and should only be used as a last resort if the webhost does not support server side scripting. (it's not common but some don't believe it or not)

 
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..

 
0
 

Here is an old (pre using JQuery) script that reads in a .csv file to make a table out of it - from a forum question a long long time ago. The XML route would be much easier to format and maintain in the long run.

It's similar to the tutorial - but uses the XMLHttpRequest to bring in the text. At least it gives another option to think about.

// JavaScript Document
// Thank you VERY much to Ben Nadel for this on his blog
// posted at 
//http://www.bennadel.com/blog/1504-Ask-Ben-Parsing-CSV-Strings-With-Javascript-Exec-Regular-Expression-Command.htm
	

if (window.XMLHttpRequest) {
		//for firefox, opera and safari browswers
		var txtFile = new XMLHttpRequest();
	}
	if (typeof XMLHttpRequest == "undefined")
  XMLHttpRequest = function () {
    try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
      catch (e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
      catch (e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (e) {}
    //Microsoft.XMLHTTP points to Msxml2.XMLHTTP.3.0 and is redundant
    throw new Error("This browser does not support XMLHttpRequest.");
  };
  
txtFile.open("GET","TestingOne.csv");
txtFile.send(false);

var allText = txtFile.responseText;

	
function clickyHere (){
	//var tar = document.getElementById('finalTable');
	var info = txtFile.responseText;
	var infoArray = [];
	infoArray = info.split(/,|,+\s|\s+,\n|\r/);
	var tableCount = info.split(/,|\$/g).length;
	var rowCount = info.split(/\r|[\r]/g).length;
	var endLine = info.split(/\$/g).length;
	var columnTry = tableCount/endLine;
	document.write("<table border='1' columns='"+columnTry+"'>");
	document.write("<tr>");
	var count;
	count = 0;
	for (i=0;i<infoArray.length;i++){
		x = infoArray[i];
		document.write("<td>"+x+"&nbsp;");
		if (count != columnTry){
			count ++;
			//document.write("</td>");
			}
		if (count >= columnTry){
			document.write("</tr><tr>");
			count = 0;
			}
	}					
			document.write("</tr></table>");
			
}

The Html code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript" src="tableTest3.js"></script>
<title>Untitled Document</title>
</head>

<body>
<form id="tablebuild">
<input type="button" value="Click me" onclick="clickyHere();" />
</form>
<div id="finalTable"></div>
</body>
</html>

I attached the original text file, but I did convert it to a csv file.

Attachments TestingOne.txt (5.97 KB)
Part Number	Part Type	Part Measure Ft.	Part Dimensions Inches	Door Type	Door Swing	Door Height	Description	Color	List Price	Unit Type	Unit Dimensions	Total Unit Cost	@Photos
SS92A003	Lock	N/A	N/A	Hinged	N/A	N/A	Lock Keeper for Single Point Lock (Deadbolt) - Frame	Nickel	$4.00 	Piece	N/A	N/A	SS92A.eps
SS92A004	Lock	N/A	N/A	Hinged	N/A	N/A	Lock Keeper for Single Point Lock (Deadbolt) - Frame	Brass	$4.00 	Piece	N/A	N/A	SS92A.eps
SS92B003	Lock	N/A	N/A	Hinged	N/A	N/A	Lock Keeper for Single Point Lock (Latch) - Frame	Nickel	$5.00 	Piece	N/A	N/A	SS92B.eps
SS92B004	Lock	N/A	N/A	Hinged	N/A	N/A	Lock Keeper for Single Point Lock (Latch) - Frame	Brass	$5.00 	Piece	N/A	N/A	SS92B.eps
SS92C003	Lock	N/A	N/A	Hinged	N/A	N/A	Lock Keeper for Single Point Lock (Deadbolt) - Panel	Nickel	$4.00 	Piece	N/A	N/A	SS92C.eps
SS92C004	Lock	N/A	N/A	Hinged	N/A	N/A	Lock Keeper for Single Point Lock (Deadbolt) - Panel	Brass	$5.00 	Piece	N/A	N/A	SS92C.eps
SS92D003	Lock	N/A	N/A	Hinged	N/A	N/A	Lock Keeper for Single Point Lock (Latch) - Panel	Nickel	$5.00 	Piece	N/A	N/A	SS92D.eps
SS92D004	Lock	N/A	N/A	Hinged	N/A	N/A	Lock Keeper for Single Point Lock (Latch) - Panel	Brass	$1.00 	Piece	N/A	N/A	SS92D.eps
MS92A001	Lock	N/A	N/A	Hinged	N/A	N/A	Keeper Base for Single Point Lock (Deadbolt)	White	$1.00 	Piece	N/A	N/A	MS92A.eps
MS92A004	Lock	N/A	N/A	Hinged	N/A	N/A	Keeper Base for Single Point Lock (Deadbolt)	Light Brown	$1.00 	Piece	N/A	N/A	MS92A.eps
MS92B001	Lock	N/A	N/A	Hinged	N/A	N/A	Keeper Base for Single Point Lock (Latch)	White	$1.00 	Piece	N/A	N/A	MS92B.eps
MS92B004	Lock	N/A	N/A	Hinged	N/A	N/A	Keeper Base for Single Point Lock (Latch)	Light Brown	$1.00 	Piece	N/A	N/A	MS92B.eps
SS930376	Lock	N/A	N/A	Hinged	Inswing	608	Multi-Point Locking System	Nickel	$215.00 	Set	N/A	N/A	SS930.eps
SS930476	Lock	N/A	N/A	Hinged	Inswing	608	Multi-Point Locking System	Brass	$215.00 	Set	N/A	N/A	SS930.eps
SS930377	Lock	N/A	N/A	Hinged	Outswing	608	Multi-Point Locking System	Nickel	$215.00 	Set	N/A	N/A	SS930.eps
SS930477	Lock	N/A	N/A	Hinged	Outswing	608	Multi-Point Locking System	Brass	$215.00 	Set	N/A	N/A	SS930.eps
SS930379	Lock	N/A	N/A	Hinged	Inswing	611	Multi-Point Locking System	Nickel	$215.00 	Set	N/A	N/A	SS930.eps
SS930479	Lock	N/A	N/A	Hinged	Inswing	611	Multi-Point Locking System	Brass	$215.00 	Set	N/A	N/A	SS930.eps
SS930380	Lock	N/A	N/A	Hinged	Outswing	611	Multi-Point Locking System	Nickel	$215.00 	Set	N/A	N/A	SS930.eps
SS930480	Lock	N/A	N/A	Hinged	Outswing	611	Multi-Point Locking System	Brass	$215.00 	Set	N/A	N/A	SS930.eps
SS930392	Lock	N/A	N/A	Hinged	Inswing	800	Multi-Point Locking System	Nickel	$238.00 	Set	N/A	N/A	SS930.eps
SS930492	Lock	N/A	N/A	Hinged	Inswing	800	Multi-Point Locking System	Brass	$238.00 	Set	N/A	N/A	SS930.eps
SS930393	Lock	N/A	N/A	Hinged	Outswing	800	Multi-Point Locking System	Nickel	$238.00 	Set	N/A	N/A	SS930.eps
SS930493	Lock	N/A	N/A	Hinged	Outswing	800	Multi-Point Locking System	Brass	$238.00 	Set	N/A	N/A	SS930.eps
SS932003	Lock	N/A	N/A	Hinged	N/A	N/A	Top & Bottom Drive for Multi-Point Locking System (Passive)	Nickel	$109.00 	Set	N/A	N/A	SS932.eps
SS932004	Lock	N/A	N/A	Hinged	N/A	N/A	Top & Bottom Drive for Multi-Point Locking System (Passive)	Brass	$109.00 	Set	N/A	N/A	SS932.eps
SS93X003	Lock	N/A	N/A	Hinged	N/A	N/A	Flush Bolt (2 pc for Single Point Lock - Passive)	Nickel	$39.00 	Piece	N/A	N/A	SS93X.eps
SS92E003	Lock	N/A	N/A	Hinged	N/A	N/A	Lock Keeper for Multi-Point Locking System - Frame	Nickel	$8.00 	Piece	N/A	N/A	SS92E.eps
SS92E004	Lock	N/A	N/A	Hinged	N/A	N/A	Lock Keeper for Multi-Point Locking System - Frame	Brass	$8.00 	Piece	N/A	N/A	SS92E.eps
SS92M003	Lock	N/A	N/A	Hinged	N/A	N/A	Lock Keeper for Multi-Point Locking System - Panel	Nickel	$8.00 	Piece	N/A	N/A	SS92M.eps
SS92M004	Lock	N/A	N/A	Hinged	N/A	N/A	Lock Keeper for Multi-Point Locking System - Panel	Brass	$8.00 	Piece	N/A	N/A	SS92M.eps
MS92E001	Lock	N/A	N/A	Hinged	N/A	N/A	Keeper Base for Multi-Point Locking System - Frame	White	$1.00 	Piece	N/A	N/A	MS92E.eps
MS92E004	Lock	N/A	N/A	Hinged	N/A	N/A	Keeper Base for Multi-Point Locking System - Frame	Light Brown	$1.00 	Piece	N/A	N/A	MS92E.eps
SS92G003	Lock	N/A	N/A	Hinged	N/A	N/A	Lock Keeper for Multi-Point Locking System (2 pc)	Nickel	$11.00 	Piece	N/A	N/A	SS92G.eps
SS92G004	Lock	N/A	N/A	Hinged	N/A	N/A	Lock Keeper for Multi-Point Locking System (2 pc)	Brass	$11.00 	Piece	N/A	N/A	SS92G.eps
MS92G001	Lock	N/A	N/A	Hinged	N/A	N/A	Keeper Base for Multi-Point Locking System (2 pc)	White	$1.00 	Piece	N/A	N/A	MS92G.eps
MS92G004	Lock	N/A	N/A	Hinged	N/A	N/A	Keeper Base for Multi-Point Locking System (2 pc)	Light Brown	$1.00 	Piece	N/A	N/A	MS92G.eps
SS93B036	Lock	N/A	N/A	Hinged	Inswing	N/A	Bottom Bolt for Multi-Point Locking System		$15.00 	Piece	N/A	N/A	SS93B.eps
SS93B037	Lock	N/A	N/A	Hinged	Outswing	N/A	Bottom Bolt for Multi-Point Locking System		$15.00 	Piece	N/A	N/A	SS93B.eps
SS93B035	Lock	N/A	N/A	Hinged	N/A	608	Top Bolt for Multi-Point Locking System		$15.00 	Piece	N/A	N/A	SS93B.eps
SS93B038	Lock	N/A	N/A	Hinged	N/A	611	Top Bolt for Multi-Point Locking System		$15.00 	Piece	N/A	N/A	SS93B.eps
SS93B051	Lock	N/A	N/A	Hinged	N/A	800	Top Bolt for Multi-Point Locking System		$15.00 	Piece	N/A	N/A	SS93B.eps
SS93B009	Lock	N/A	N/A	Hinged	In/Outswing	N/A	Top Bolt for Single-Point Locking System		$15.00 	Piece	N/A	N/A	SS93B.eps
SS93B010	Lock	N/A	N/A	Hinged	Inswing	N/A	Top Bolt for Single-Point Locking System		$15.00 	Piece	N/A	N/A	SS93B.eps
SS93B011	Lock	N/A	N/A	Hinged	Outswing	N/A	Top Bolt for Single-Point Locking System		$15.00 	Piece	N/A	N/A	SS93B.eps
SS916000	Lock	N/A	N/A	Hinged	N/A	N/A	Flush Bolt Alignment Bracket		$1.00 	Piece	N/A	N/A	SS916.eps
SS92P003	Lock	N/A	N/A	Hinged	Inswing	N/A	Multi-Point Top/Bottom Keeper Plate - Frame (2 pc)	Nickel	$27.00 	Piece	N/A	N/A	SS92P.eps
SS92P004	Lock	N/A	N/A	Hinged	Inswing	N/A	Multi-Point Top/Bottom Keeper Plate - Frame (2 pc)	Brass	$27.00 	Piece	N/A	N/A	SS92P.eps
SS92H003	Lock	N/A	N/A	Hinged	Outswing	N/A	Multi-Point Top/Bottom Keeper Plate - Frame (2 pc)	Nickel	$27.00 	Piece	N/A	N/A	
SS92H004	Lock	N/A	N/A	Hinged	Outswing	N/A	Multi-Point Top/Bottom Keeper Plate - Frame (2 pc)	Brass	$27.00 	Piece	N/A	N/A
 
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?

 
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!

Question Answered as of 3 Years Ago by tawes01, Sogo7, macgurl70 and 2 others
 
0
 

macgurl70,
I would change this
txtFile.send(false);
like so:
txtFile.send(null);

 
0
 

This is great except that the CSV data will come out as unquoted so when i run something like indexOf(7) it cannot read strings.
what can you add to that RegEx so that the items are "quoted"

thanks.
 
0
 

thanks stbuchok but the reason why i'm looking at this javascript solution is because i have to be compatible with IE 7.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: