繁体   English   中英

Electron - 创建文件时出现问题,错误“EROFS:只读文件系统”

[英]Electron - problem creating file, error “EROFS: read-only file system”

嗯,我正在开发一个可以改善公司工作的应用程序。 为此,我需要在没有对话框的情况下创建、保存和读取文件。

我在文档和互联网的帮助下创建了这段代码:

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();

我有一个问题,因为如果脚本在本地版本上工作,在 MacOS 上构建应用程序(使用电子构建器)后,控制台中会出现错误:“写入数据时出现问题!”。 显示后出现错误内容:Error: EROFS: read-only file system, open ‘appdata.json’。

我检查了权限,我检查了其他位置 - 仍然相同:(我正在网上寻找解决方案,但不幸的是没有解决问题。

有没有人遇到过这样的问题?

建成后。 资源将被打包在asar文件中,但这只是read-only 您不能修改asar的文件。

如果我是你。 appData.json存储在Application Support 我认为这是应用程序的流行配置。

您可以使用它获取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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM