I'm trying to understand file I/O in Javascript and this is puzzling me. I have a file input which loads a file from disk then I use a reader to grab the contents. But when I repeat the process, the file will not read a second time or third time. This is the code:
<html>
<head>
</head>
<body>
<input type="file" id="myFile">
<script type="text/javascript">
window.onload = function() {
var input = document.getElementById("myFile");
var output = document.getElementById("output");
input.addEventListener("change", function() {
if (this.files && this.files[0]) {
var myFile = this.files[0];
var reader = new FileReader();
reader.addEventListener('load', function(e) {
console.log(e.target.result);
});
reader.readAsText(myFile);
}
});
}
</script>
</body>
</html>
When I read the file the first time, console.log prints e.target.result. When I try reading the file a second time, it will not print. Even if I change the contents of the file. But it will print a new file I load into it. Is it possible to load the same file twice?