// File Reader.jsl // Roger C. garrett // 3/9/2010 // FileReader CONSTRUCTOR function FileReader(fileURL) { this.fileURL = fileURL; this.oRequest = new XMLHttpRequest(); this.characterIndex = 0; this.atEOF = false; } function FileReader.prototype.open() { // Returns true if success in opening, otherwise returns false. // Note that the open status is available via the status method. this.oRequest.open("GET", this.fileURL, false); // false means to wait for the response. this.oRequest.setRequestHeader("User-Agent",navigator.userAgent); this.oRequest.send(null); if (this.oRequest.status != 200) { return false; } alert("File contains "+this.oRequest.responseText.length+" characters:\n\n"+this.oRequest.responseText); return true; } function FileReader.prototype.status() { return this.oRequest.status; } function FileReader.prototype.atEOF() { // Returns true if readLine has resulted in it being ator beyond the End Of File. return this.atEOF; } function FileReader.prototype.readLine() { // Reads and returns one line of text. // The atEOF function can be called to determine whether the entire file has been read. var text = ""; var character = ""; while (this.characterIndex < this.oRequest.responseText.length) { character = this.oRequest.responseText.charAt(this.characterIndex); ++this.characterIndex; if ((character == "\n") || (character == "\r")) { break; } text += character; } if (this.characterIndex >= this.oRequest.responseText.length) { this.atEOF = true; } return text; } function FileReader.prototype.close() { // ACtually, there is no close method for XMLHttpRequest class. //this.oRequest.close(); } function onClickFileRead() { var fileName = "http://www.ThunderbirdTechnology.com/MathTest/JavaScript/FileReaderTest.txt"; var fileReader = new FileReader(fileName); if (!fileReader.open()) { alert("File open failure.\n\n"+fileName); return; } var text = fileReader.readLine(); alert(text); text = fileReader.readLine(); alert(text); text = fileReader.readLine(); alert(text); fileReader.close(); } /********* This is a programthat strips out comments ad other irrelevant stuff from javascript files. http://wareseeker.com/Web-Development/javascript-minimizer-1.0.zip/266379 http://wareseeker.com/free-javascript-file-access/ *********/ /** http://www.javascripter.net/faq/xmlhttpr.htm In this example, when you click the Send Request button, the browser requests additional data from the server (the file requested_file.htm), and then displays the returned data in a message box, by executing the following JavaScript code: var oRequest = new XMLHttpRequest(); var sURL = "http://"+self.location.hostname+"/faq/requested_file.htm"; oRequest.open("GET",sURL,false); oRequest.setRequestHeader("User-Agent",navigator.userAgent); oRequest.send(null) if (oRequest.status==200) alert(oRequest.responseText); else alert("Error executing XMLHttpRequest call!"); In most modern browsers, the general syntax of the XMLHttpRequest.open() method is as follows: RCG: NOTE it's the oRequest.responseText that has the data in it!!!! var oRequest = new XMLHttpRequest(); oRequest.open( method, URL, async ) The parameters method, URL, async have the following meaning: method Specifies the method to be used for the HTTP request. Can be one of the following: "GET", "POST", "HEAD". URL Specifies the URL to which the request will be sent when the XMLHttpRequest.send() method is executed. async Boolean. If true, specifies that the HTTP request should be performed asynchronously, that is, without forcing the code execution to wait until the request's results are returned to the browser. Note that the XMLHttpRequest.open() method itself does not send the request to the Web server. It is the XMLHttpRequest.send() method that actually sends the request. Note also that in older browsers, e.g. in Microsoft Internet Explorer 6 or earlier, you might not be able to instantiate the XMLHttpRequest using the above simple syntax: new XMLHttpRequest(). Instead, depending on the actual client configuration, you'd have to use one of the following statements: oRequest = new ActiveXObject("Msxml2.XMLHTTP"); oRequest = new ActiveXObject("Microsoft.XMLHTTP"); **/