2013-03-10 45 views
3

我想将存储在phonegap文件夹(www/default_files/file.txt)中的文件复制到iOS中的文档文件夹,到目前为止我所得到的是:使用PhoneGap将文件从www复制到'文档'

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) 
{ 
    fileSys.root.getFile("file.txt", null, function(theFile){ // file exist don't do anything}, 
    function(error) 
    { 
     // file does not exist, so copy it 
     fileSys.copyTo(fileSys.root.fullPath, "www/default_files/file.txt", 
     function success(entry) 
     { 
      console.log("Success: " + entry); 
     }, 
      function error(entry) 
     { 
      console.log(entry); 
     }); 
    }); 
}, fileError); 

fileSys.root.fullPath包含正确的文件路径,问题是如何访问www文件夹...

任何想法?

回答

1

您可以像下面那样访问www文件夹
/var/mobile/Applications/35=9087/yourapp.app/www/yourfolder/yourfilename。转

编辑
使用alert(window.location.pathname);找到你的应用程序的路径。

5

此代码适用于iOS上的我。此功能将数据库文件的副本从/ www文件夹实现到/ Documents文件夹。

function copyBase(){ 
    var wwwPath = window.location.pathname; 
    var basePath = 'file://'+ wwwPath.substring(0,wwwPath.length-10); 
    window.resolveLocalFileSystemURL(basePath+'myDB.db', 
     function(fileDB){ 
      console.log('success! database was found') 
      window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, null); 

       function onSuccess(fileSystem) { 
        var documentsPath = fileSystem.root; 
        fileDB.copyTo(documentsPath, 'myDB.db', 
        function(){ 
         console.log('copying was successful') 
        }, 
        function(){ 
         console.log('unsuccessful copying') 
        }); 
       } 
     }, 
     function(){ 
      console.log('failure! database was not found') 
     }); 
} 
+1

您保存了我的日子 – allemattio 2015-05-29 12:35:40

+0

您可以使用'cordova.file.applicationDirectory'而不是hacky www-path。 – 2017-10-01 11:39:19

0

使用cordova-plugin-file。 然后您可以使用以下字符串访问www文件夹:cordova.file.applicationDirectory +“www /” 和Documents文件夹使用字符串: cordova.file.documentsDirectory。

相关问题