I'm working on a project that requires submitting files through a web page. Although it's not necessary, it's convenient for the user to be able to edit the file after already having selected it. However, I ran into the problem that reading the FileReader returns null after making changes and then saving them. My fairly straightforward debugging code is below.
<html>
<head>
</head>
<body>
<input type="file" id="file" value>
<button id="sub">Submit</button>
</body>
<script>
document.getElementById("sub").addEventListener("click", function() {
var reader = new FileReader();
reader.addEventListener('loadend', function() {
var src_code = this.result;
console.log(src_code);
});
if (document.getElementById("file").files.length == 0) {
//empty
alert("empty");
}
else {
console.log(document.getElementById("file").files[0]);
reader.readAsText(document.getElementById("file").files[0]);
}
});
</script>
</html>
I read another question here that claims FileReader is at fault for this but, unfortunately, I absolutely need the text within the file and the answer did not provide an alternative.
Is there a way to get the text from a file that lets the user update the file after selecting it?