Electron function to read a local file - FS - Not reading(电子函数读取本地文件 - 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 - 不读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:电子函数读取本地文件 - FS - 不读取
基础教程推荐
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 动态更新多个选择框 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01