电子函数读取本地文件 - FS - 不读取

Electron function to read a local file - FS - Not reading(电子函数读取本地文件 - FS - 不读取)
本文介绍了电子函数读取本地文件 - FS - 不读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我需要让电子读取本地文件时,我有一个电子项目.

I have an electron project when I need to get electron to read a local file.

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

Right now what I have is this, where it loads and displays the contents of a html file.

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

I just need it to read a file and store it on a variable for now.

这是我当前的 main.js:

Here is my current 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 文件的内容

How can I do this as it's not showing the contents of the README.md file in the console.log

推荐答案

基本上你需要做以下几件事.

Basically you need to do the following things.

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");
});

更多阅读请访问 这里 :) 谢谢..

For more read please visit here :) Thanks..

还有一件事要添加..请检查您的文件路径是否正确.你可以做类似下面的事情.

One more thing to add..Please check that your path to file is correct. You could do something similar to below.

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

这篇关于电子函数读取本地文件 - FS - 不读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)