Possible Duplicate:
Reading a txt file from Javascript
I need to open and read the contents of a txt file using JavaScript. How can I do this?
Possible Duplicate:
Reading a txt file from Javascript
I need to open and read the contents of a txt file using JavaScript. How can I do this?
On a remote server you'd just use an XMLHttpRequest
object to retrieve the data and access it.
var request = new XMLHttpRequest();
request.open('GET', URL TO FILE);
request.onreadystatechange(function(){
if(request.readyState === 4 && request.statusCode === 200){
console.log(request.responseText);
}
});
request.send();
That will fail in IE7, 8 and probably 9.
If you're trying to read from local disk, you're going to have a bad time.
If the text file is in your local file system, you'll have a hard time doing so because of the restrictions of Javascript in accessing one's local file system. If the text file is in a server, you can fetch the contents of the file with a simple XHR (Ajax call).