Last active
December 21, 2015 15:19
-
-
Save nczz/6325875 to your computer and use it in GitHub Desktop.
HTML5 Read File Example / HTML5 讀檔範例
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 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