2015-04-06 52 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。 –

相關問題