Reading a Text File using JavaScript

I don’t work with Web applications very often. But recently I’ve been taking a close look at a pre-release version of the Bing Maps 8 library. As part of that investigation, I decided to look at reading a text file using JavaScript.


In the old days, you’d have to use an ActiveX control to read a file. But now reading a text file is quite easy.

The key JavaScript code is:

function ReadFile()
  var file = fileInput.files[0];
  var reader = new FileReader();
  reader.onload = function(e) {
    displayArea.innerText = reader.result;

Here ‘fileInput’ is the ID of an HTML input type=file tag, and ‘displayArera’ is the ID of an HTML pre tag.

Very nice.


This entry was posted in Miscellaneous. Bookmark the permalink.