Hello. I have a very important question. I am currently attempting to create a Javascript file to test the HTML5 File API. First of all, I want to dynamically obtain a local file using Javascript and Google Chrome. I have succesfully done that by using a function which returns a XMLHttpRequest() object.

However, when I try to access the content of it, I get empty space.

function getAsText(readFile) {
  var reader = new FileReader();
  // Read file into memory as UTF-16      
  reader.readAsText(readFile, "UTF-16");
  // Handle progress, success, and errors
  reader.onprogress = updateProgress;
  reader.onload = loaded;
  reader.onerror = errorHandler;
getAsText(x); //Where x is the XMLHTTPRequest object

This is the code. When the alert function is called, I get an empty window. Can someone please tell me if this is possible to fix? I want to get a dynamic reference to a file in the getAsText function (the readFile parameter SHOULD be a valid file). Pardon my bad English.

6 Years
Discussion Span
Last Post by Veneficvs

As far as I can tell from the W3C spec, the FileReader functions are asynchronous. That means that the alert() happens before the file is done loading.


Alright, I understand that. But that <pre> item still does not get populated with the content of the file.I think it is because fileReader only works with files and not with xml objects... Any ideas?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.