Skip to content

Instantly share code, notes, and snippets.

@nczz
Last active December 21, 2015 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nczz/6325875 to your computer and use it in GitHub Desktop.
Save nczz/6325875 to your computer and use it in GitHub Desktop.
HTML5 Read File Example / HTML5 讀檔範例
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>File IO</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script>
$(function() {
if (window.File && window.FileReader && window.FileList && window.Blob) {
// Great success! All the File APIs are supported.
} else {
alert('The File APIs are not fully supported in this browser.');
}
function handleFileSelect(evt) {
var file = evt.target.files[0]; // FileList object
var reader = new FileReader();
var text = '';
reader.onload = (function(thefile) {
return function(e) {
text = e.target.result;
text = text.replace(/[a-zA-Z]+/g, '').replace(/[\n\r\ ]+/g, "\n");
$('#tmp').text(text);
};
})(file);
reader.readAsText(file);
}
$('[name=files]').on('change', handleFileSelect);
});
</script>
</head>
<body>
<input type="file" name="files"><pre id="tmp"></pre>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment