【问题标题】:FileReader & FileWriter issues (cordova 2.0.0 js)FileReader & FileWriter 问题 (cordova 2.0.0 js)
【发布时间】:2012-08-15 03:05:38
【问题描述】:

我一直在为 Android* 设备编写一段 PhoneGap (cordova-2.0.0.js) 代码,以保留一些数据。我收到了这个奇怪的错误代码,并且文件似乎没有被写入。我从示例代码开始,可以成功地将内联字符串写入文件句柄,因此我确定我的权限和所有权限都是正确的。

也许我没有正确处理所有回调?有很多要听的!可能是 truncate(0);我很难找到很多关于它的文档。我需要多次调用 window.requestFileSystem 吗?我做了 2 次来注册不同的回调。如果不是这样,是什么导致了错误?

我们也很乐意接受有关减少读取 + 写入操作的总行数的建议...

*运行 Android 2.3.4 的模拟器

这是我的代码:

var CREDENTIALS_FILE_NAME = "credentials.json";
var credentials;

// INIT -- Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, initCredentialReader, fail);
}
function initCredentialReader(fileSystem) {
    fileSystem.root.getFile(CREDENTIALS_FILE_NAME, {create: true}, gotFileEntryReader, fail); 
}
function gotFileEntryReader(fileEntry) {
    fileEntry.file(gotFileToRead, fail);
}
function gotFileToRead(file){
    var reader = new FileReader();
    reader.onloadend = function(e) {
        console.log("--FILE READER: "+e.target.result);

        if( e.target.result.length < 1 ) {
            credentials = newCredentials();
        } else {
            credentials = JSON.parse( e.target.result );
        }
    };
    reader.readAsText(file);
}
// END CHAIN

function initCredentialWriter(fileSystem) {
    fileSystem.root.getFile(CREDENTIALS_FILE_NAME, {create: true}, gotFileEntryWriter, fail); 
}
function gotFileEntryWriter(fileEntry) {
    fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
    writer.onwrite = function(e) {
        console.log("--- write success");
    };
    var toWrite = JSON.stringify(credentials);
    console.log("--- toWrite: "+toWrite);
    writer.truncate(0);
    writer.seek(0);
    writer.write(toWrite);
}


function fail(error) {
    console.log("--- write FAIL: "+error.code);
}

function newCredentials() {
    console.log("returning newCredentials!");
    return {
        "username" : "",
        "password" : "",
        "organization" : "",
        "cookieValue" : "" };
}

function getCredentials() {
    console.log("--- getCredentials: "+credentials);
    return credentials;
}

function saveCredentials( jsonCredentials ) {
    console.log('--- saveCredentials jsonCredentials: '+ jsonCredentials);

    credentials = JSON.stringify( jsonCredentials );
    console.log('--- credentials to save: '+credentials)

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, initCredentialWriter, fail);
    return credentials;
} 

错误

08-14 18:41:38.839:I/Web 控制台(2678):成功回调错误: 文件9 = {"code":7,"line":2863,"expressionBeginOffset":91407,"expressionEndOffset":91455,"sourceId":4122528,"sourceURL":"file:///android_asset/www/cordova-2.0.0 .js"} 在文件:///android_asset/www/cordova-2.0.0.js:258

【问题讨论】:

    标签: javascript android cordova


    【解决方案1】:

    所以,事实证明调用 truncate() 和 write() 不是异步正确的——只是实现了更多回调,如下所示:

    function initCredentialTruncate(fileSystem) {
        fileSystem.root.getFile(CREDENTIALS_FILE_NAME, {create: true}, gotFileEntryTruncate, fail); 
    }
    function gotFileEntryTruncate(fileEntry) {
        fileEntry.createWriter(gotFileTruncate, fail);
    }
    function gotFileTruncate(writer) {
        writer.onwrite = function(e) {
            console.log("--- truncate success");
        };
        writer.truncate(0);
        //writer.seek(0);
    }
    // END CHAIN
    

    并在必要时调用 init 函数。谢谢你让我发泄,StackOverflow...

    【讨论】:

      猜你喜欢
      • 2018-02-15
      • 2011-10-16
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      相关资源
      最近更新 更多