Articles → JAVASCRIPT → Read Text File In Javascript Using Filereader
Read Text File In Javascript Using Filereader
Text File
Read Text File
- Add a file upload control
- Add a reference of jQuery
- Add file upload change event
- Use FileReader object to read text file.
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function () {
$("#upload").change(function (evt) {
for (var i = 0, f; f = evt.target.files[i]; i++) {
var reader = new FileReader();
reader.onload = function (evt) {
alert(evt.target.result);
};
reader.readAsText(f);
}
});
});
</script>
</head>
<body>
<form>
<input type="file" id="upload" />
</form>
</body></html>
Output