Read file contents using javascript

by Mohan 2014-03-03 19:08:39


<h1>Read file contents using javascript</h1>


Here is sample code to read any file contents using javascript.
<br />


<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];

if (f) {
var r = new FileReader();
r.onload = function(e) {
var contents = e.target.result;
alert( "Got the file.n"
+"name: " + f.name + "n"
+"type: " + f.type + "n"
+"size: " + f.size + " bytesn"
+ "starts with: " + contents.substr(1, contents.indexOf("n"))
);
}
r.readAsText(f);
} else { alert("Failed to load file"); }
}
document.getElementById('fileinput').addEventListener('change', readSingleFile, false);
</script>

493
like
0
dislike
0
mail
flag

You must LOGIN to add comments