Created
August 16, 2010 07:47
-
-
Save kcliu/526587 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<meta http-equiv="Content-type" content="text/html;charset=UTF-8"> | |
<title>Show File Data</title> | |
<style type='text/css'> | |
body { | |
font-family: sans-serif; | |
} | |
</style> | |
<script type='text/javascript'> | |
function loadFile() { | |
var input, file, fr; | |
if (typeof window.FileReader !== 'function') { | |
bodyAppend("p", "The file API isn't supported on this browser yet."); | |
return; | |
} | |
input = document.getElementById('fileinput'); | |
if (!input) { | |
bodyAppend("p", "Um, couldn't find the fileinput element."); | |
} | |
else if (!input.files) { | |
bodyAppend("p", "This browser doesn't seem to support the `files` property of file inputs."); | |
} | |
else if (!input.files[0]) { | |
bodyAppend("p", "Please select a file before clicking 'Load'"); | |
} | |
else { | |
file = input.files[0]; | |
fr = new FileReader(); | |
fr.onload = receivedText; | |
fr.readAsText(file); | |
} | |
function receivedText() { | |
showResult(fr, "Text"); | |
fr = new FileReader(); | |
} | |
} | |
function showResult(fr, label) { | |
var markup, result, n, aByte, byteStr; | |
markup = []; | |
result = fr.result; | |
markup.push(result); | |
bodyAppend("p", label + " (" + result.length + "):"); | |
bodyAppend("pre", markup.join(" ")); | |
} | |
function bodyAppend(tagName, innerHTML) { | |
var elm; | |
elm = document.createElement(tagName); | |
elm.innerHTML = innerHTML; | |
document.body.appendChild(elm); | |
} | |
</script> | |
</head> | |
<body> | |
<form action='#' onsubmit="return false;"> | |
<input type='file' id='fileinput'> | |
<input type='button' id='btnLoad' value='Load' onclick='loadFile();'> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment