Simone M
Simone M

Reputation: 687

How to create subfolder using Cordova

I'm using Cordova and I tried to create a folder to the root of my SD card on a device. I used the following code for create the folder and add a file 'login.txt' inside it:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

function gotFS(fileSystem) {
   fileSystem.root.getDirectory("citylook", {create: true}, gotDir);
}

function gotDir(dirEntry) {
    dirEntry.getFile("login.txt", {create: true, exclusive: true}, gotFile);
}

function gotFile(fileEntry) {
    // Do something with fileEntry here
    fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {
    writer.onwriteend = function(evt) {
        console.log("contents of file now 'some sample text'");
        writer.truncate(11);
        writer.onwriteend = function(evt) {
            writer.seek(0);
            writer.write(testo);
            writer.onwriteend = function(evt){
                console.log("contents of file now '"+testo+"'");
            }
        };
    };
    writer.write("some sample text");
}

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

Now, I need to create a folder inside "citylook" folder, so I tried this:

function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, null);
}

function onSuccess() {
    fileSystem.root.getDirectory("citylook", {create: true, exclusive: false}, onGetDirectoryWin, onGetDirectoryFail);
    fileSystem.root.getDirectory("citylook/nuovo", {create: true, exclusive: false}, onGetDirectoryWin2, onGetDirectoryFail2);
}

but I can't create a subfolder. What's is wrong in my code? Thank you.


SOLVED:

fileSystem.root.getDirectory("citylook", {create: true}, gotDir);
fileSystem.root.getDirectory("citylook/subfolder", {create: true}, gotDir);

Upvotes: 2

Views: 2900

Answers (2)

Tran Hoang Hiep
Tran Hoang Hiep

Reputation: 574

window.resolveLocalFileSystemURL(
        cordova.file.externalDataDirectory,
        function(entry) {
          entry.getDirectory(
            "myNewDirectory",
            { create: true, exclusive: false },
            function(result) {
              alert(JSON.stringify(result));
            },
            onError
          );
        }
      );

i'm using cordova create a folder to the Android/data//files success.

Upvotes: 2

Ranjith
Ranjith

Reputation: 11

var type = window.PERSISTENT;
   var size = 5*1024*1024;

   window.requestFileSystem(type, 0, successCallback, errorCallback);
   function successCallback(fs) {
       var dir=fs.root;
       alert('root   -'+dir.fullPath);// O/P:-root   -/
      dir.getDirectory('Albums', {create: true}, function(dirEntry) {
          alert('Albums Path:'+dirEntry.fullPath);// O/P:-Allbums Path: /Albums/
         dirEntry.getDirectory('Images',{create:true},function(subDir){
                              alert('Hello');
                              alert('SubDirPath='+subDir.fullPath);//output:-SubDirPath=/Albums/Images/
             //subDir.getFile('Log.txt',{create: true, exclusive: false}, function(fileEntry) {

        //writeFile(fileEntry, null, isAppend);
                 //alert('File Path'+fileEntry.fullPath);

    }, onErrorCallback);

         },errorCallback);
          dirEntry.getDirectory('Audio',{create:true},function(subDir){
                              alert('Hello');
                              alert('SubDirPath='+subDir.fullPath);//output:-SubDirPath=/Albums/Audio/
         },errorCallback);
      }, errorCallback);
   }
        function errorCallback(error) {
      alert("ERROR: " + error.code)
   }

This code helps to create folders and sub-folders in root. /Albums is the main folder. /Albums/Images/ and /Albums/Audio/ are the sub-folders(Images and Audio)

Upvotes: 0

Related Questions