Copying a file using Cordova

Burcu Saner picture Burcu Saner · Jul 21, 2015 · Viewed 9.3k times · Source

I've been trying to copy file named versions.txt from applicationDirectory to externalApplicationStorageDirectory using cordova but code fails.

here is the code

var path = cordova.file.applicationDirectory + "www/Data/versions.txt";

      window.resolveLocalFileSystemURL(path,
      function gotFile(fileEntry)
      {

          window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
              function onSuccess(fileSystem)
              { 
                      var directory = new DirectoryEntry("versions.txt", path);

                      fileEntry.copyTo(directory, 'versions.txt',
                          function()
                          {
                              alert('copying was successful')
                          },
                          function()
                          {
                              alert('unsuccessful copying')
                          });

              }, null);
      },null);

Any help?

Answer

javatogo picture javatogo · Mar 7, 2018

The fileEntry.copyTo somehow always gave error, so I tried the fileEntry.moveTo as below, which works well for me in copying any file from the www into say the Library folder of the iOS device.

function copyToLocation(fileName){       
console.log("Copying :"+fileName);
    window.resolveLocalFileSystemURL(cordova.file.applicationDirectory+"www/"+fileName,function (fileEntry)
    {
        window.resolveLocalFileSystemURL(cordova.file.dataDirectory,function (directory)
        {                  
           fileEntry.moveTo(directory, 'new_fileName.txt',function(){
                alert('Successful Copy!');
            },
            function()
            {
                alert('Copying Unsuccessful ');
            });
        },null);
    }, null);
 }