I'm trying to read a local file with javascript and a Google Chrome App (it may not be possible through Chrome I think), but I can't see what's the latest approach to the problem.
I can read it with the following code:
obj.read = function() {
return new Promise(function(resolve){
var xmlhttp = new XMLHttpRequest();
var file_path = 'file_name.xml';
xmlhttp.open('GET', file_path, true);
xmlhttp.send(null);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
let xml = xmlhttp.responseText;
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xml, "text/xml");
console.log(xml);
console.log(xmlDoc);
resolve(xmlDoc);
}
}
});
};
But it is like I should be using something like
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
console.log(e.target.result);
};
})(file_path);
var file_path = 'file_name.xml';
var file_parts = [
new Blob(['you construct a file...'],{type: 'text/plain'}),
'Same way as you do with blob',
new Uint16Array([33])
];
var file = new File(file_parts, file_path);
reader.readAsText(file);
(copied from https://stackoverflow.com/a/24495213/826815) (I'm having a hard time finding literature on this topic)
So, what's the way to do it?