I have been struggling with reading local files from the file path in javascript
. I have tried XMLHttpRequest
to no avail.
function readFile(file) {
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4) {
if (rawFile.status === 200 || rawFile.status == 0) {
var text = rawFile.responseText;
document.write(text);
}
}
}
rawFile.send(null);
}
window.onload = function() {
readFile("text.txt");
}
I have also tried FileReader
but the way I understand it is that it doesn't read files from a string file path; or how can I create a File
object to use in FileReader
. I also don't want to use node fs module
. Could anyone help out on the best and surest way to read local files from the file path? THANKS