+
80
-

Electron如何读取本地文件并显示?

Electron如何读取本地文件并显示?

网友回复

+
0
-

在index.html中添加一个按钮,每按一次从本地读取一次文件:

<button id="btn1">按钮1</button>

修改preload.js:
// preload.js
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
window.addEventListener('DOMContentLoaded', () => {
省略号
  let readAndDisplay = () => {
    let fs = require("fs");
    fs.readFile('local_file.txt', (err, data) => {
      if (err) return console.error(err);
      console.log(data.toString());
      let myNode = document.createTextNode(data.toString());
      document.body.insertBefore(myNode, document.body.firstChild);
    });
  }

  document.querySelector('#btn1').addEventListener("click", event => {
    readAndDisplay();
  })
省略号
})

我知道答案,我要回答