Local open the file using the FileReader

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
Here Insert Picture Description

Published 54 original articles · won praise 41 · views 110 000 +

Guess you like

Origin blog.csdn.net/weixin_42966484/article/details/103979721