javascript - 读取本地文件的 Electron 功能 - FS - 不读取

标签 javascript node.js electron

当我需要获取 Electron 来读取本地文件时,我有一个 Electron 项目。

现在我拥有的是这个,它加载并显示 html 文件的内容。

我现在只需要它来读取文件并将其存储在变量中。

这是我当前的 main.js:

 const {app, BrowserWindow } = require('electron');
const path = require('path');
const url = require('url');
var fs = require('fs');

let mainWindow;

function createNewWindow() {
  mainWindow = new BrowserWindow({
    width: 1300,
    height: 1000,
    minWidth: 600,
    minHeight: 400,
    title: 'Test App'
  })
}

function loadInitialUrl() {
  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }))
}

function closeApplication() {
  mainWindow.on('closed', () => {
    mainWindow = null;
})
}


app.on('ready', function(){
  createNewWindow();
  loadInitialUrl();
  mainWindow.setMenu(null);
  mainWindow.openDevTools();
  fs.readFile('./README.md', 'utf8', function (err,data) {
    if (err) {
      return console.log(err);
    }
    console.log(data);
  });
  mainWindow.on('closed', function() {mainWindow = null;});
});

我该怎么做,因为它没有在 console.log 中显示 README.md 文件的内容

最佳答案

基本上您需要做以下事情。

1.加载需要的依赖

var remote = require('remote'); // Load remote compnent that contains the dialog dependency
var dialog = remote.require('dialog'); // Load the dialogs component of the OS
var fs = require('fs'); // Load the File System to execute our common tasks (CRUD)

2.读取文件内容

dialog.showOpenDialog((fileNames) => {
    // fileNames is an array that contains all the selected
    if(fileNames === undefined){
        console.log("No file selected");
        return;
    }

    fs.readFile(filepath, 'utf-8', (err, data) => {
        if(err){
            alert("An error ocurred reading the file :" + err.message);
            return;
        }

        // Change how to handle the file content
        console.log("The file content is : " + data);
    });
});

3.更新现有文件内容

 var filepath = "C:/Previous-filepath/existinfile.txt";// you need to save the filepath when you open the file to update without use the filechooser dialog againg
var content = "This is the new content of the file";

fs.writeFile(filepath, content, (err) => {
    if (err) {
        alert("An error ocurred updating the file" + err.message);
        console.log(err);
        return;
    }

    alert("The file has been succesfully saved");
});

更多阅读请访问here :) 谢谢..

还有一件事要补充..请检查您的文件路径是否正确。您可以执行类似于下面的操作。

var path = require('path');
var p = path.join(__dirname, '.', 'README.md');

关于javascript - 读取本地文件的 Electron 功能 - FS - 不读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43722450/

相关文章:

javascript - Electron 编译器编译后显示白页

javascript - 匹配并替换 Javascript 中每出现 "Second"次“,”

mysql - 是否可以从 Electron 应用程序将查询发送到在线数据库?

javascript - 如何在react中推送不完整的部分

java - 如何从 Tapestry 代码调用对话框上的 javascript?

javascript - 获取对现有浏览器窗口的引用

javascript - 我们如何为网页实现可重用的焦点管理器

node.js - 函数外部的 Nodejs 变量值

javascript - 方法是否占用 javascript 类实例中的空间?

angularjs - 缺少脚本 : start Electron