2016-02-05 69 views
6

我正在使用Cordova FileTransfer對象將文件從url下載到設備。使用Cordova FileTransfer將文件下載到設備的下載文件夾

var fileTransfer = new FileTransfer(); 
var path = cordova.file.dataDirectory; 
fileTransfer.download(
     fileUrl, 
     path + "/sample.pdf", 
     function(theFile) { 
      console.log("download complete: " + theFile.toURI()); 
      alert("File downloaded to "+cordova.file.dataDirectory); 
     }, 
     function(error) {    
      console.log(JSON.stringify(error)); 
     } 
    ); 

在這種情況下,文件下載到data/data/com.fileDemo/files/ (我不知道下載是否是成功的,因爲我不能訪問此文件夾。獲得成功的消息download complete: file:///data/data/com.fileDemo/files/sample.pdf)。 如何使用相同的方法將文件下載到android設備的「Downloads」文件夾中?

回答

4

在科爾多瓦,與FileTransfer,你可以要求TEMPORARYPERSISTENT文件系統

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, gotFS, fail); 
  • 的iOS
    • PERSISTENT將返回文檔目錄,
    • TEMPORARY將返回緩存目錄
  • 的Android
    • PERSISTENT將返回SD卡/手機內存
    • TEMPORARY根將返回數據文件夾內的文件夾。

參考File API & FileTransfer獲取更多信息。

+4

我剛剛嘗試過,但gotFs指向應用程序的數據文件夾。而不是根目錄下的下載文件夾。可以訪問根目錄下的下載文件夾嗎? –

+1

對於那些仍然存在這個問題的人來說,無論是'cordova.file.externalRootDirectory +'Download /''還是'fileSystem.root.toURL()+'Download / – REI

相關問題