I want to read a text file using Javascript. For passing the FileList Object
, the available codes use the <input type="file">
with change event, but i want the path of the txt file to be hardcoded inside javascript and the event to be fired onload of document. How can I implement it?? The available code with <input type="file">
is:
<input type="file" id="fileinput" />
<script type="text/javascript">
function readSingleFile(evt) {
//Retrieve the first (and only!) File from the FileList object
var f = evt.target.files[0];
console.log(evt);
if (f) {
var r = new FileReader();
r.onload = function(e) {
var contents = e.target.result;
alert( "Got the file."+f);
}
r.readAsText(f);
} else {
alert("Failed to load file");
}
}
document.getElementById('fileinput').addEventListener('change', readSingleFile, false);
</script>