I'm trying to use the HTML5 FileReader to load a CSV file, but its onLoad function never triggers (tried with Firefox and Chrome). This is despite its result property being populated with the file's contents, its readyState being set to 2 (DONE), and its error property remaining null. It also produces no console errors. My code is below:
var reader = new FileReader();
function process_file(file) {
reader.onError = function(evt) {upload_failed("There was an error opening your CSV file. It may be corrupted.")};
reader.onAbort = function(evt) {upload_failed("There was an error opening your CSV file. It may be corrupted.")};
reader.onLoad = upload_complete;
reader.readAsText(file);
alert('Started');
}
function upload_complete(evt) {
alert('Complete');
csv_data = reader.result;
$submit_button.prop('disabled', false);
}
function upload_failed(message) {
alert('Failed');
$submit_button.prop('disabled', true);
alert(message);
}
"Started" is the only alert which shows. The other properties of the reader were accessed through the Firefox console. The process_file function is run through:
<input type='file' onchange="process_file(this.files[0])" style="display: none;" accept=".csv" "id and name etc..."/>
I would like to know what I'm doing wrong and how to have the onLoad function trigger when the file is loaded.