Using the HTML5 file API I have created a mechanism for the user to select a file from his computer, which is then read as a string and processed within the application. The code, however, doesn't work in IE9, so I'm looking for a solution that does. This is my code, which creates a file reader object:
function CreateFileReader(element)
{
var self=this;
// create an input field and insert it into the document
this.element=element;
this.element.html('');
var fileBox=$('<input type="file"/>');
this.element.append(fileBox);
// when the contents (file) of the fileBox change, read the file
this.fileBox.change(function () {
if (this.files.length > 0){
if (this.files[0]!=undefined) {
var file=this.files[0];
// set up the file reader
var reader = new FileReader();
reader.file=file;
// specify what happens when the file is loaded
reader.onloadend = self.processFile;
// read the file as a text string
reader.readAsText(file);
}
}
});
}
CreateFileReader.prototype.processFile = function(e) {
// if the file was loaded successfully
if (e.target.error==null && e.target.readyState==2) {
var fileString=e.target.result;
// do some stuff with fileString here
}
}
I'd be grateful if you could suggest alternatives that work in IE9.