Skip to content

Instantly share code, notes, and snippets.

@LuoZijun
Last active August 29, 2015 14:26
Show Gist options
  • Save LuoZijun/15f0eba08091309b92a1 to your computer and use it in GitHub Desktop.
Save LuoZijun/15f0eba08091309b92a1 to your computer and use it in GitHub Desktop.
读取 二进制文件
<html>
<head>
<script type="text/javascript">
// 使用方法:
// 页面加载完毕后,打开 浏览器控制台,调用函数 `get_file_content()` 即可以看到数据。
function chr (code){
return String.fromCharCode(parseInt(code));
}
function ord(s){
return s.charCodeAt(0);
}
function get_file_content (){
var dom = document.getElementById("haha");
var files = dom.files;
var reader = new FileReader();
var file = files[0];
file.path = dom.value;
var data = {
"result": "",
"info": {
"name": file.name,
"path": file.path,
"size": file.size,
"type": file.type,
"lastModified": file.lastModified,
"lastModifiedDate": file.lastModifiedDate
}
};
reader.onload = function (evt){
// console.log(evt.target.result);
data.result = evt.target.result;
result = [];
for ( var i=0; i<data.result.length; i++ ){
result.push(ord(data.result[i]));
}
data.result = JSON.stringify(result);
console.info("File Content: ");
console.log(data);
};
reader.readAsBinaryString(file); // readAsText readAsDataURL
}
</script>
</head>
<body>
<input type="file" id="haha" />
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment