Skip to content

Instantly share code, notes, and snippets.

@tchap
Last active August 29, 2015 14:03
Show Gist options
  • Save tchap/330f57cbf0de5f525561 to your computer and use it in GitHub Desktop.
Save tchap/330f57cbf0de5f525561 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<body>
<form>
<input id="fileInput" type="file"></input>
</form>
<span id="fileContent"></span>
<script type="text/javascript">
var apiAvailable = function() {
return window.File && window.FileReader;
};
document.getElementById("fileInput").addEventListener("change", function(e) {
if (!apiAvailable()) {
alert("The File API is not available, go away!");
return;
}
var files = e.target.files;
if (files.length == 0) {
return;
}
var file = files[0];
var reader = new FileReader();
reader.onload = function() {
var result = reader.result;
document.getElementById("fileContent").innerHTML = "<pre>" + result + "</pre>";
};
reader.readAsBinaryString(file);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment