Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying a file using Cordova

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?

like image 272
Burcu Saner Avatar asked Jul 21 '15 19:07

Burcu Saner


2 Answers

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);
 }
like image 169
javatogo Avatar answered Sep 28 '22 00:09

javatogo


I finally got a functioning broader no specific/case function to copy a file having a baseFileURI to a temporary (fileSystem == LocalFileSystem.TEMPORARY) or persistent (fileSystem == LocalFileSystem.PERSISTENT) APP directory, the destiny file having the name destPathName.

It uses the cordova-plugin-file plugin.

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, for example, like this

copyFile("file:///storage/emulated/0/Android/data/com.form.parking.violation/cache/IMG-20180505-WA0004.jpg",         
        "myImg.jpg",
        LocalFileSystem.TEMPORARY); 
like image 30
João Pimentel Ferreira Avatar answered Sep 28 '22 00:09

João Pimentel Ferreira