[英]Electron - problem creating file, error “EROFS: read-only file system”
Well, I'm working on a certain app that would improve work in the company.嗯,我正在开发一个可以改善公司工作的应用程序。 For this I would need to create, save and read a file without a dialog box.为此,我需要在没有对话框的情况下创建、保存和读取文件。
I created this code with the help of documentation and the Internet:我在文档和互联网的帮助下创建了这段代码:
const electron = require('electron');
let fs = require('fs'), app = electron.remote;
let localData, fileName = "appdata.json";
function loadAppData() {
fs.readFile(fileName, (err, data) => {
if (err) {
console.log("There was a problem reading the data!");
// console.log(err);
} else {
console.log("Data loaded!");
localData = JSON.parse(data);
console.log(localData);
}
});
}
function saveAppData(content) {
content = JSON.stringify(content);
fs.writeFile(fileName, content, (err) => {
if (err) {
console.log("There was a problem saving data!");
// console.log(err);
} else {
console.log("Data saved correctly!");
loadAppData();
}
});
}
function initappData() {
if (fs.existsSync(fileName)) {
console.log("File detected, loading");
loadAppData();
} else {
let defData = {
"patients": [],
"actions": [],
"visits": []
};
console.log("No file! I create! Saving! Loading!");
saveAppData(defData);
}
}
initappData();
And I have a problem because if the script works on the local version, after the application build on MacOS (using electron-builder) the error appears in the console: "There was a problem writing data!".我有一个问题,因为如果脚本在本地版本上工作,在 MacOS 上构建应用程序(使用电子构建器)后,控制台中会出现错误:“写入数据时出现问题!”。 After displaying the error content appears: Error: EROFS: read-only file system, open 'appdata.json'.显示后出现错误内容:Error: EROFS: read-only file system, open ‘appdata.json’。
I checked permissions, I checked in other locations - still the same :( I was looking for a solution on the net but unfortunately nothing solved the problem.我检查了权限,我检查了其他位置 - 仍然相同:(我正在网上寻找解决方案,但不幸的是没有解决问题。
Has anyone encountered such a problem?有没有人遇到过这样的问题?
After the build.建成后。 The resource will be packaged inside asar
file But this is just read-only
.资源将被打包在asar
文件中,但这只是read-only
。 You can't modify the file inside of asar
.您不能修改asar
的文件。
If I were you.如果我是你。 I'm going to store the appData.json
at Application Support
.我appData.json
存储在Application Support
。 And I think this is the popular configure for application.我认为这是应用程序的流行配置。
You can get the Application Data
path by using this.您可以使用它获取Application Data
路径。
function getAppDataPath() {
switch (process.platform) {
case "darwin": {
return path.join(process.env.HOME, "Library", "Application Support", "Your app name");
}
case "win32": {
return path.join(process.env.APPDATA, "Your app name");
}
case "linux": {
return path.join(process.env.HOME, ".Your app name");
}
default: {
console.log("Unsupported platform!");
process.exit(1);
}
}
}
function saveAppData(content) {
const appDatatDirPath = getAppDataPath();
// Create appDataDir if not exist
if (!fs.existsSync(appDatatDirPath)) {
fs.mkdirSync(appDatatDirPath);
}
const appDataFilePath = path.join(appDatatDirPath, 'appData.json');
content = JSON.stringify(content);
fs.writeFile(appDataFilePath, content, (err) => {
if (err) {
console.log("There was a problem saving data!");
// console.log(err);
} else {
console.log("Data saved correctly!");
loadAppData();
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.