JavaScript中使用FileReader读取word文件内容
转载于:https://blog.csdn.net/dj7858177/article/details/107106360 如有侵权,请联系我删除
<html>
<head>
<meta http-equiv="content-type" content="text/JavaScript;charset=utf-8" />
</head>
<input type="file" id="files" name="file" />读取文件:
<span class="readButton">
<button>显示</button>
</span>
<body>
<script>
function readBlob() {
var files = document.getElementById('files').files;
if (!files.length) {
alert('Please select a file!');
return;
}
var file = files[0];
var start = 0;
var stop = file.size - 1;
var reader = new FileReader();
reader.onloadend = function(evt) {
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
var content = evt.target.result;
alert(content);
}
};
var blob = file.slice(start, stop + 1);
reader.readAsText(blob,"utf-8");
}
document.querySelector('.readButton').addEventListener('click', function(evt) {
if (evt.target.tagName.toLowerCase() == 'button') {
readBlob();
}
}, false);
</script>
</script>
</body>
</html>