Software Archive
Read-only legacy content
17061 Discussions

Cordova File Plugin doesn't work in AppPreview!?

Oliver_L_1
Beginner
471 Views

Hey there,
I marked cordovas "File"-Plugin to be included in my APP.

On "Emulate" in JS "cordova.file" exists, but all Settings are "null". When using AppPreview on my iOS / Windows Phone (doesn't matter if using "Server Apps" or connected device via USB and "Debug" tab) "cordova.file" is undefined...

WHAT can I do?

0 Kudos
1 Reply
John_H_Intel2
Employee
471 Views

This should get you going. Make sure you have enabled the file plugin and pushed your app to the cloud.

 

        FILE<br>
        <button onclick="writeFile();">write file</button><br>
        <p id="fileWritten"></p>
        <button onclick="readFile();">read file</button><br>
        <p id="fileRead"></p>

 

// file write stuff
    function writeFile()
    {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);        
    }
    
    function gotFS(fileSystem) {
        fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
    }

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

    function gotFileWriter(writer) {
        console.log("success");
        var element = document.getElementById('fileWritten');
        element.innerHTML = "file written successfully";
        
        /*writer.onwriteend = function(evt) {
            console.log("contents of file now 'some sample text'");
            writer.truncate(11);
            writer.onwriteend = function(evt) {
                console.log("contents of file now 'some sample'");
                writer.seek(4);
                writer.write(" different text");
                writer.onwriteend = function(evt){
                    console.log("contents of file now 'some different text'");
                }
            };
        };*/
        var d = new Date();
        var n = d.toString();
        writer.write("jph was here!" + n + "<br>");
    }

    function fail(error) {
        console.log(error.code);
        var element = document.getElementById('fileWritten');
        element.innerHTML = "file write FAILED";
        
    }
    // read file
    function readFile()
    {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFSRead, failRead);
    }
    
    function gotFSRead(fileSystem) {
        fileSystem.root.getFile("readme.txt", null, gotFileEntryRead, failRead);
    }

    function gotFileEntryRead(fileEntry) {
        //alert("read success");
        var element = document.getElementById('fileRead');
        element.innerHTML = "file read successfully";
        fileEntry.file(gotFileRead, failRead);
    }

    function gotFileRead(file){
        readAsText(file);
        //readDataUrl(file);
    }

    function readDataUrl(file) {
        var reader = new FileReader();
        reader.onloadend = function(evt) {
            console.log("Read as data URL");
            console.log(evt.target.result);
        };
        reader.readAsDataURL(file);
    }

    function readAsText(file) {
        var reader = new FileReader();
        reader.onloadend = function(evt) {
            console.log("Read as text");
            console.log(evt.target.result);
            //alert(evt.target.result);
            var element = document.getElementById('fileRead');
            element.innerHTML += "<br>" + evt.target.result;
        };
        reader.readAsText(file);
    }

    function failRead(error) {
        console.log("error code = " + error.code);
        alert("read failed, error code = " + error.code);
    }

0 Kudos
Reply