Search code examples
javascriptandroidcordovacordova-pluginsandroid-file

Where are files stored with cordova-file-plugin


I'm trying to create a excel on a mobile device, I'm testing with android but it should work for iOS too

I used the following code from the documentation Documentation

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
    console.log('file system open: ' + fs.name);
    fs.root.getFile("newPersistentFile.txt", { create: true, exclusive: false }, function (fileEntry) {

        console.log("fileEntry is file?" + fileEntry.isFile.toString());
        fileEntry.name == 'someFile.txt'
        fileEntry.fullPath == '/someFile.txt'
        fileEntry.createWriter(function (fileWriter) {

            fileWriter.onwriteend = function() {
                console.log("Successful file write...");
                fileEntry.file(function (file) {
                    var reader = new FileReader();

                    reader.onloadend = function() {
                        console.log("Successful file read: " + this.result);
                        //displayFileData(fileEntry.fullPath + ": " + this.result);
                    };

                    reader.readAsText(file);

                },);
            };

            fileWriter.onerror = function (e) {
                console.log("Failed file write: " + e.toString());
            };

            let dataObj = new Blob(['some file data'], { type: 'text/plain' });


            fileWriter.write(dataObj);
        });

    });

});

I've tried changing the first three lines for the following ones, with the same result

window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (fs) {
    console.log('file system open: ' + fs.name);
    fs.root.getFile("newPersistentFile.txt", { create: true, exclusive: false }, function (fileEntry) { ...

I get the following console log

file system open: persistent    
fileEntry is file?true    
Successful file write...    
Successful file read: some file data

so, the file is created and I can read it, but I don't get any prompt or something, then I navigate to my file on Android/data/com.myapp.app/files and I don't have any file


Solution

  • Seems the files were saving but I couldn't see them, but I could read them via cordova-file-plugin

    I changed the destination folder to

    let ruta = cordova.file.externalRootDirectory
    let directoryRoute = "myApp";
                window.resolveLocalFileSystemURL(ruta, function (fs) {
                    fs.getDirectory(directoryRoute, { create: true }, function (fs2) {
                        fs2.getFile(fileName, { create: true, exclusive: false }
    , function (fileEntry) { ...
    

    with cordova.file.externalRootDirectory I'm creating if doesn't exist a folder to save my app documents, this works with android, probably there will be changes to iOS

    I'm going to update the answer on a few days when I have the answer for iOS in case this can help someone