I have a javascript function in which I would like to read an entire text file from my local computer. And store the entire file in a javascript variable. Then be able to return this variable to another function. I've researched different sites that said I could use XMLHttpRequest method to do so and I've tried using an absolute file path and relative file path. I just want a new set of eyes on the issue just in case I'm missing something smmall or completely off. The code is below
function setNewTexts()
{
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "textfile.txt", true);
txtFile.onreadystatechange = function()
{
if (txtFile.readyState === 4) { // document is ready to parse.
if (txtFile.status === 200) { // file is found
allText = txtFile.responseText;
lines = txtFile.responseText.split("\n");
}
}
}
txtFile.send(null);
var text = "go go go ";
return textFile;
}
value = setNewTexts();
<input type="file"/>, you can then use FileReader to output the contents.file:///?