使用Cordova复制文件

问题描述 投票:3回答:4

我一直在尝试使用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);

有帮助吗?

cordova fileapi
4个回答
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);

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

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

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); 
© www.soinside.com 2019 - 2024. All rights reserved.