Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用OS.file在文件中持久存储JSON_Javascript_Firefox_Firefox Addon_Firefox Addon Sdk - Fatal编程技术网

Javascript 使用OS.file在文件中持久存储JSON

Javascript 使用OS.file在文件中持久存储JSON,javascript,firefox,firefox-addon,firefox-addon-sdk,Javascript,Firefox,Firefox Addon,Firefox Addon Sdk,如何在firefox插件sdk中以独立于操作系统的方式使用OS.file在文件中持久存储JSON? 例如,如果我将文件保存在windows中的D:\file中,它在linux中甚至在没有驱动器D的windows上都无法工作 如何执行此操作?要将realObject作为JSON存储在文件MyFileName.JSON中,该文件在当前配置文件目录下的扩展数据目录中创建/覆盖,您可以执行以下操作: Components.utils.import("resource://gre/modules/File

如何在firefox插件sdk中以独立于操作系统的方式使用OS.file在文件中持久存储JSON? 例如,如果我将文件保存在windows中的D:\file中,它在linux中甚至在没有驱动器D的windows上都无法工作


如何执行此操作?

要将
realObject
作为JSON存储在文件
MyFileName.JSON
中,该文件在当前配置文件目录下的
扩展数据
目录中创建/覆盖,您可以执行以下操作:

Components.utils.import("resource://gre/modules/FileUtils.jsm");
Components.utils.import("resource://gre/modules/NetUtil.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");

let asJSON = JSON.stringify(realObject,null);
let file2 = openFileInPrefsExtensionData("MyFileName.json");
overwriteTextFileFromString (file2, asJSON);


/**
 * Open file in Extension's extension-data directory under the pref Directory.
 *   This is the correct place to store files for your extension under the profile's
 *   directory.
 */
function openFileInPrefsExtensionData(filename) {
    let fileNameList = ["extension-data","MyExtensionID"];
    fileNameList.push(filename);
    //This does create all directories along the way to the file,
    //  but the file itself is not created.
    return FileUtils.getFile("ProfD", fileNameList);
}


/**
 * Overwrite a file from a string.
 *  Currently this just overwrites, without any callback function at the end
 *  of the write.  If the write fails, then it is reported in the console,
 *  but not otherwise handled.
 */
function overwriteTextFileFromString(file,data) {
    overwriteTextFile(file, data, function (status) {
    });
}

/**
 * Overwrite a file with a string.
 */
function overwriteTextFile(nsiFile, data, flags, callback) {
    //data is data you want to write to file
    //if file doesnt exist it is created
    // You can also optionally pass a flags parameter here. It defaults to
    // FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE;

    //PR_RDONLY         0x01    Open for reading only.
    //PR_WRONLY         0x02    Open for writing only.
    //PR_RDWR           0x04    Open for reading and writing.
    //PR_CREATE_FILE    0x08    If the file does not exist, the file is created. If the file exists, this flag has no effect.
    //PR_APPEND         0x10    The file pointer is set to the end of the file prior to each write.
    //PR_TRUNCATE       0x20    If the file exists, its length is truncated to 0.
    //PR_SYNC           0x40    If set, each write will wait for both the file data and file status to be physically updated.
    //PR_EXCL           0x80    With PR_CREATE_FILE, if the file does not exist, the file is created. If the file already exists, no action and NULL is returned.

    let args = Array.prototype.slice.call(arguments,4);

    if(typeof flags == "undefined") {
        //These are already the defaults.
        flags = FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE;
    }
    //XXX NOTE: Flags is currently not being used.  I don't recall why I disabled its use.

    var ostream = FileUtils.openSafeFileOutputStream(nsiFile);
    var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
    converter.charset = "UTF-8";
    var istream = converter.convertToInputStream(data);
    // The last argument (the callback) is optional.
    //asyncCopy automatically closes both the input and output streams upon completion.
    NetUtil.asyncCopy(istream, ostream, function (status) {
        if (!Components.isSuccessCode(status)) {
            // Handle error!
            Components.utils.reportError('error on write isSuccessCode = ' + status);
            return;
        }
        // Data has been written to the file.
        //send the status, and any other args to the callback function.
        args.unshift(status);
        if(typeof callback == "function" ) {
            //there is a callback function..
            callback.apply(callback, args);
        }
    });
}

您是否出于特定原因使用NetUtil?或者OS.File可以代替它使用吗?@Noitidart,这是我大约一年前组装的东西。它主要来源于MDN上的示例代码。NetUtil主要用于从UTF-8转换器进行复制,该转换器将
JSON.stringify
转换为UTF-8。重新编码以使用OS.File并非不合理。坦白地说,自从最初实现了它的功能并对其进行了测试(后来进行了几次测试,但它仍然保持了功能性)以来,我并没有对它考虑太多。