I have an electron project when I need to get electron to read a local file.
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.
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;});
});
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.Loading required dependencies
2.Read file content
3.Update existing file content
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.