Repository navigation
Electron App reading Data from User provided Json file #13008
Replies: 1 comment
|
There are two different questions here, and putting them in different places is what makes this behave properly:
Keeping them "accessible and editable" by disabling asar and writing into The pattern that works: ship the template inside the app, copy it out on first run, then only ever touch the copy: // electron-main
const { app } = require("electron");
const fs = require("fs/promises");
const path = require("path");
async function userDataFile(name) {
const dir = app.getPath("userData");
const target = path.join(dir, name);
const template = path.join(app.getAppPath(), "resources", name); // inside asar - readable, never written
try { await fs.access(target); }
catch { await fs.mkdir(dir, { recursive: true }); await fs.copyFile(template, target); }
return target;
}And in the Vue page, no // electron-preload
contextBridge.exposeInMainWorld("files", {
readJson: (name) => ipcRenderer.invoke("file:read-json", name),
writeJson: (name, data) => ipcRenderer.invoke("file:write-json", name, data),
});In main, resolve by name against the userData directory rather than accepting a path: ipcMain.handle("file:read-json", async (_e, name) => {
const file = await userDataFile(String(name));
return JSON.parse(await fs.readFile(file, "utf8"));
});
If the file genuinely must be "provided" by the user rather than shipped, use |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I am building an Electron App using Quasar V1, and I would like it to load a User provided JSON file (stored under \resources\app at the end of the day in the Electron package - I don't use asar format in order to keep access to these resource files).
How can I do that from a vue page? Where to put this json file so that it remains accessible and editable by the end user?
Thanks a lot for any help!
David
All reactions