使用Cordova复制文件

时间:2015-07-21 19:47:46

标签: cordova fileapi

我一直在尝试使用cordova将applicationDirectory中名为versions.txt的文件复制到externalApplicationStorageDirectory,但代码失败。

这是代码

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);

任何帮助?

4 个答案:

答案 0 :(得分:2)

现在它可以正常工作,目标目录应该被解析。

这是解决方案

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);

答案 1 :(得分:2)

fileEntry.copyTo总是给出错误,所以我尝试了如下的fileEntry.moveTo,这对我来说很有效,可以将www中的任何文件复制到iOS设备的Library文件夹中。

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);
 }

答案 2 :(得分:1)

将我的源代码提供给PhoneGap开发人员应用程序。我的文件复制代码如下。

   //COPY FILE
   var wwwDirEntry;

   //resolve url for directory entry for putting in copied file
   window.resolveLocalFileSystemURL(cordova.file.dataDirectory+'phonegapdevapp/www/', function success(dirEntry) {
       wwwDirEntry = dirEntry;
   });

   //resolve file URL to file entry to enable copying
   //
   //Desired URL: file:///data/user/0/com.adobe.phonegap.app/files/phonegapdevapp/www/my_awesome_file.doc
   //BASE URL: cordova.file.dataDirectory / file:///data/user/0/com.adobe.phonegap.app/files/
   //
   //alert(JSON.stringify(cordova.file.dataDirectory));
   //
   window.resolveLocalFileSystemURL(cordova.file.dataDirectory+'phonegapdevapp/www/my_awesome_file.doc',
      function onSuccess(fileEntry)
      {
          //alert(JSON.stringify(fileEntry));
          fileEntry.copyTo(wwwDirEntry, 'a_copy_of_my_awesome_file.doc',
          function()
          {
              alert('copying was successful');
          },
          function()
          {
              alert('copying FAILED');
          });
     }, function (e) { alert(JSON.stringify(e)); });

答案 3 :(得分:0)

我终于得到了一个功能更广泛的没有特定/案例功能来将具有baseFileURI的文件复制到临时(fileSystem == LocalFileSystem.TEMPORARY)或持久(fileSystem == LocalFileSystem.PERSISTENT)APP目录,命运文件名称为destPathName

它使用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')
        });
}

使用此功能,例如,像这样

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