2015-04-06 51 views
2

我已经看到了几个不同的示例,说明如何使用Phonegap File API将文件从一个目录复制到另一个目录,但是无法将文件从我的应用程序文件夹复制到根目录中的现有文件夹。 File API文档非常模糊,我一直在应用程序目录中找不到文件。将文件从应用程序文件夹复制到JQM/Phonegap App中的根文件夹?

我可以访问SD卡根文件夹文件:/// storage/emulated/0 /使用root.toURL()并读写它,我似乎无法访问我的应用程序文件夹。

Phonegap构建配置文件的权限。

<gap:plugin name="org.apache.cordova.file" version="1.3.1" /> 
<preference name="iosExtraFilesystems" value="library,library-nosync,documents,documents-nosync,cache,bundle,root" /> 
<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,root" /> 

任何帮助或例子会很好。

感谢,

RG

wwwPath = cordova.file.applicationDirectory; 
var fp = "temp/myfile.txt"; 
var fileDestPath = "tempFolder/"; 

function copyFile(){ 
    var basePath = wwwPath+fp; 
    window.resolveLocalFileSystemURL(basePath, function(fileEntry){ 
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) { 
       var copyToPath = fileSystem.root.toURL()+fileDestPath; 
       fileEntry.copyTo(copyToPath, 'newFileName.txt', function(){ 
        alert("file copy success");       
        },fileCopyFail); 
      },fileCopyFail); 
    },fileCopyFail); 


function fileCopyFail(error) { 
    alert(error); 
} 

回答

2

看起来像你想做到这一点在Android和iOS设备;事情在每个平台上都有点不同。使用cordova-plugin-file插件:

o Android应用程序文件夹(file:///android_asset/)的别名是cordova.file.applicationDirectory。您可能已经知道,Android SD卡根(file:///storage/emulated/0/)的别名为cordova.file.externalRootDirectory

o iOS应用程序文件夹(appname.app/)的别名是cordova.file.applicationDirectory

显然没有外部存储写在iOS上,但取决于你想要什么用文件做,谁需要访问它,你可以写你的文件(Documents/ - >cordova.file.documentsDirectory)或库(Library/NoCloud/ - >cordove.file.dataDirectory)文件夹。

+0

感谢回复迈克。现在,只是试图让android工作。我正在使用file.applicationDirectory,但不能解决文件URL。 API没有提供任何有关如何从应用程序文件夹复制到外部根目录的实例。任何机会你有一些代码?我会发布我一直在努力的工作。 – BGecko

4

您可以使用文件传输,下载和存储文件到不同的位置:

// get base url for index.html 
var baseUrl = location.href.replace("/index.html", ""); 
var fp = "temp/myfile.txt"; 
var fileDestPath = "tempFolder/"; 
function copyFile(){ 
    var sourceFilePath = baseUrl + "/" +fp; 
    var targetFilePath = fileSystem.root.toURL()+fileDestPath; 

var ft = new FileTransfer(); 
ft.download(
       sourceFilePath, 
       targetFilePath, 
       function(entry){ 
        alert("file copy success")  
       }, 
       function(error){ 
        alert(error); 
       } 
     ); 

正如我最近发现,这不会在Windows 81的工作,但我成功地测试了Android和iOS。

+0

我总是得到错误代码为空,这也可能与MP3文件? –

+0

完美!很好,谢谢。 copyTo函数总是让我错误代码1000。 –

相关问题