JohnnyAce
JohnnyAce

Reputation: 3739

Copy a file from www to 'Documents' using PhoneGap

I am trying to copy a file that is stored on the phonegap folder (www/default_files/file.txt) to the documents folder in iOS, so far what I got is:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys)
{
  fileSys.root.getFile("file.txt", null, function(theFile){ // file exist don't do anything},
    function(error)
    {
      // file does not exist, so copy it
      fileSys.copyTo(fileSys.root.fullPath, "www/default_files/file.txt",
        function success(entry)
        {
          console.log("Success: " + entry);
        },
          function error(entry)
        {
          console.log(entry);
        });
  });
}, fileError);

fileSys.root.fullPath contains the correct Documents Path, the problem is how to access the www folder...

Any ideas?

Upvotes: 4

Views: 2620

Answers (3)

javatogo
javatogo

Reputation: 308

Use the cordova-plugin-file. Then you can access the www folder using the string : cordova.file.applicationDirectory+ "www/" and Documents folder using the string: cordova.file.documentsDirectory.

Upvotes: 0

Emmett
Emmett

Reputation: 464

This code works for me on iOS. This function implements a copy of the database file from /www folder to /Documents folder.

function copyBase(){
    var wwwPath = window.location.pathname;
    var basePath = 'file://'+ wwwPath.substring(0,wwwPath.length-10);
    window.resolveLocalFileSystemURL(basePath+'myDB.db', 
        function(fileDB){
            console.log('success! database was found')  
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, null);

                function onSuccess(fileSystem) {
                    var documentsPath = fileSystem.root;
                    fileDB.copyTo(documentsPath, 'myDB.db',
                    function(){
                        console.log('copying was successful')
                    }, 
                    function(){
                        console.log('unsuccessful copying')
                    });
                }
        }, 
        function(){
            console.log('failure! database was not found')
        });
}

Upvotes: 4

user1885162
user1885162

Reputation: 91

you can access the www folder as below
/var/mobile/Applications/35----------------9087/yourapp.app/www/yourfolder/yourfilename.ext

edit
use alert(window.location.pathname); to find the path of your application.

Upvotes: 1

Related Questions