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;
  alert(reader.result);
}
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.

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.