vendredi 30 octobre 2015

phoneGap file read/write not working

I have followed all examples I have found but none seems to work. Instead of getting the text I just get null.

I just can't figure out what I am doing wrong!

The following is a code sample I have stitched together for a couple of examples found online.

function saveSettings() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFSw, fail);
}

function gotFSw(fileSystem) {
    fileSystem.root.getFile("tdb.settings", {create: true, exclusive: false}, gotFileEntryw, fail);
}

function gotFileEntryw(fileEntry) {
    fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {
        var username = document.getElementById("username").value;
        var password = document.getElementById("password").value;
    writer.write(username + "\n" + password);
}

function readSettings()
{
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFSr, fail);
}
function gotFSr(fileSystem) {
    fileSystem.root.getFile("tdb.settings", null, gotFileEntryr, fail);
}

function gotFileEntryr(fileEntry) {
    fileEntry.file(gotFiler, fail);
}
function gotFiler(file){
    readAsText(file);
}
function readAsText(file) {
    var reader = new FileReader();
    reader.onloadend = function(evt) {
        console.log("Read as text");
        console.log(evt.target.result);
    };
    alert(file);
    alert(reader.readAsText(file));
    alert(reader.result);
    alert("done!");
}

function fail(evt) {
    console.log(evt.target.error.code);
}

I am using saveSettings and readSettings as entry points.

Aucun commentaire:

Enregistrer un commentaire