Copying a file using Cordova

I am trying to copy a file called versions.txt from applicationDirectory to externalApplicationStorageDirectory using cordova, but the code is not working.

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?

+3


source to share


3 answers


now it works, target directory should be resolved.

here is the solution



window.resolveLocalFileSystemURL(cordova.file.externalApplicationStorageDirectory,
                      function onSuccess(dirEntry)
                      {
                          //alert(JSON.stringify(dirEntry));

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

      

+2


source


The Entry.copyTo file somehow always gave an error, so I tried the Entry.moveTo file as shown below, which works well for me when copying any file from www to the Library folder on 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);
 }

      

+1


source


Finally I got a more functional function, not related to specific / random, to copy a file from baseFileURI

into a temporary ( fileSystem == LocalFileSystem.TEMPORARY

) or permanent ( fileSystem == LocalFileSystem.PERSISTENT

) APP directory, a destiny file having a name destPathName

.

It uses a plugin cordova-plugin-file

.

function copyFile(baseFileURI, destPathName, fileSystem){
    window.resolveLocalFileSystemURL(baseFileURI, 
        function(file){        
            window.requestFileSystem(fileSystem, 0, 
                function (fileSystem) {
                    var documentsPath = fileSystem.root;
                    console.log(documentsPath);
                    file.copyTo(documentsPath, destPathName,
                    function(res){                        
                        console.log('copying was successful to: ' + res.nativeURL)
                    }, 
                    function(){
                        console.log('unsuccessful copying')
                    });
                });
        }, 
        function(){
            console.log('failure! file was not found')
        });
}

      

Use this function like this

copyFile("file:///storage/emulated/0/Android/data/com.form.parking.violation/cache/IMG-20180505-WA0004.jpg",         
        "myImg.jpg",
        LocalFileSystem.TEMPORARY); 

      

0


source







All Articles