The first step in the html add a button to open the file
<input type="file" id="loadFile" />
The second step, through the event listener for the implementation of the appropriate method to open the file
function readSingleFile(e) {
var file = e.target.files[0];
//如果文件不存在,就直接返回
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
//得到相应的打开文件内容
var contents = e.target.result;
};
reader.readAsText(file);
}
document.getElementById('loadFile').addEventListener('change', readSingleFile, false);
So to OK