2015-02-06 48 views
0

使用phonegap build for iOS平臺,下載過程不起作用! 有人能告訴我爲什麼下面的代碼返回的錯誤信息:Phonegap iOS將文件從聯機文件下載到本地文件系統不起作用

("download error source " + error.source); 

無法從網上 //下載圖像如:http://www.sushikoapp.com/img/branches/thumbs/big_sushiko-bchamoun.jpg

("upload error code" + error.code); sometimes 1 and sometimes 3 

的代碼如下:

var ios_directoryEntry = fileSystem.root; 
ios_directoryEntry.getDirectory("branches", { create: true, exclusive: false }, onDirectorySuccessiOS, onDirectoryFailiOS); 
var ios_rootdir = fileSystem.root; 
//var ios_fp = ios_rootdir.fullPath; 
var ios_fp = ios_rootdir.toURL(); 
ios_fp = ios_fp + "branches/" ; 

//ios_fp = "cdvfile://localhost/persistent/branches/"; 

var fileTransfer = new FileTransfer(); 
fileTransfer.download(encodeURI(imgURL + "branches/thumbs/big_sushiko-bchamoun.jpg"), ios_fp + "big_big_sushiko-bchamoun.jpg", 
        function (entry) { 
         alert("download complete: " + entry.fullPath); 
         } 
        }, 
       function (error) { 
        //Download abort errors or download failed errors 
        alert("download error source " + error.source); 
        alert("upload error code" + error.code); 
       } 
     ); 

謝謝您的建議...

回答

0

問題是,如果文件API操作實際上是異步的,那麼您將文件API操作視爲同步操作。 您可以使用cordova.file來引用文件系統上的目標文件夾(請參閱此處:https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md)。

因此,嘗試這樣的事:

resolveLocalFileSystemURL(cordova.file.documentsDirectory, onGetDocuments, function(error) { 
    console.error("Error getting Documents directory: "+error.code); 
}); 

function onGetDocuments(entry) { 
    console.log("Got Documents directory"); 
    entry.getDirectory("branches", { create: true, exclusive: false }, onGetBranches, function(error) { 
     console.error("Error creating/getting branches directory: "+error.code); 
    }); 
} 

function onGetBranches(entry){ 
    console.log("Got branches directory"); 
    doFileTransfer(entry.toURL()); 
} 

function doFileTransfer(ios_fp){ 
    var fileTransfer = new FileTransfer(); 
    fileTransfer.download(encodeURI(imgURL + "branches/thumbs/big_sushiko-bchamoun.jpg"), ios_fp + "big_big_sushiko-bchamoun.jpg", 
     function (entry) { 
      alert("download complete: " + entry.fullPath); 
      } 
     }, 
     function (error) { 
      //Download abort errors or download failed errors 
      alert("download error source " + error.source); 
      alert("upload error code" + error.code); 
     } 
    ); 
} 
+0

好吧,我們會嘗試此過程... – 2015-02-09 10:08:15

+0

我得到的警報下面:錯誤獲取文檔目錄:5 //代碼5 – 2015-02-09 13:09:52

+0

沒有在上下文中看到更多的代碼,很難知道是什麼導致了該錯誤。你是否在deviceready事件觸發後調用resolveLocalFileSystemURL? – DaveAlden 2015-02-09 14:10:33

0

您正在嘗試將文件寫入iOS根文件系統,這是不可能的,因爲iOS中的所有應用程序都是沙箱,只能訪問他們自己的沙箱。

所以不要使用fileSystem.root而是fileSystem.documents

+0

我們已經改變了fileSystem.root到fileSystem.documents,但仍然沒有工作... – 2015-02-09 10:24:41

相關問題