Cordova-写入和读取文件

Bob*_*b-M 5 ios cordova

我的cordova应用程序出现问题,在更新应用程序后,将清除localStorage。我现在正尝试使用cordova的FileWriter和FileReader将一些基本的用户数据保存到文本文件中。

在某些时候,cordova已更新并采用了这些方法,现在需要使用插件来读取和写入文件:https : //cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html

因为我的实时iOS应用程序使用旧版本的cordova,所以它必须使用FileWriter编写。我的应用程序更新(不实时)使用的是Cordova的较新版本,因此必须使用插件。

------找到了可能的解决方法,请参阅下面的更新------

尝试读取文件时,我在xcode中看到以下错误:

017-01-26 17:58:52.990 MyApp [40355:2892997]错误:插件“文件”中未定义方法“ hello there” 2017-01-26 17:58:52.991 MyApp [40355:2892997]-[CDVCommandQueue executePending ] [第142行] FAILED pluginJSON = [“ File1875336264”,“ File”,“ hello there”,[“ cdvfile://localhost/persistent/player_data.txt”,null,null]]

注意:为了方便起见,我在运行iOS模拟器时在Safari控制台中运行以下代码

我写文件的代码

(function() {
var onFail = function(err) {
    alert('write action failed!');
    alert(err.code);
};

var onGotFS = function(fileSystem) {
    alert( 'gotFS' );
    fileSystem.root.getFile("player_data.txt", {create: true}, onGotFileEntry, onFail);
};

var onGotFileEntry = function(fileEntry) {
     alert( 'gotFileEntry, path: ' + fileEntry.fullPath );
     fileEntry.createWriter(onGotFileWriter, onFail);
};

var onGotFileWriter = function(writer) {
    alert( 'gotFileWriter' );
    writer.onwrite = onFileWritten;
    writer.onerror = onFail;

    var data = "hello there";
    alert( 'writing data: ' + data );
    writer.write( data );
};

var onFileWritten = function(evt) {
    alert( "saveTokenToFile SUCCESS!" );
};

// start process of looking up file system and writing
alert( 'requesting file system ...' );
window.requestFileSystem(LocalFileSystem.PERSISTENT, 1024, onGotFS, onFail);
Run Code Online (Sandbox Code Playgroud)

})();

我读取文件的代码:

(function() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
    fs.root.getFile("player_data.txt", { create: true }, function (fileEntry) {
        fileEntry.file(function (file) {
            var reader = new FileReader();
            reader.onloadend = function() {
                alert("Success");
                console.log(evt.target.result);
            };

            reader.onerror = function() {
                console.log("reader error");                
            };

            reader.readAsText(file);
        }, onErrorReadFile);
    });
}, onErrorLoadFs);

var onErrorReadFile = function(){
    console.log("error reading file");
}

var onErrorLoadFs = function() {
    console.log("request file system has failed to load.");
}
Run Code Online (Sandbox Code Playgroud)

})();

更新

万一其他人遇到这个问题,我确实找到了一种读取保存文件的方法。fileEntry对象具有保存文件的URL路径。要访问文件上的数据,我将该URL传递给jQuery.getJSON,这使我们返回了一些可读的json。

    readDataFromFile: function(){
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
        fs.root.getFile("player_data.txt", {}, function (fileEntry) {
            var entryURL = fileEntry.nativeURL;
            jQuery.getJSON(entryURL, function(data) {
                console.log(data);
            }).fail(function(){
                console.log("file found, but contents were empty");
            });
        }, onErrorGetFile);
    }, onErrorLoadFs);

    var onErrorLoadFs = function() {
        console.log("request file system has failed to load.");
    };

    var onErrorGetFile = function() {
        console.log("requested file can not be read or does not exist");
    };
}
Run Code Online (Sandbox Code Playgroud)