我們必須使用PhoneGap Build和Apache Cordova爲iOS和Android構建應用程序。 Phonegap版本是3.5.0。並且我們希望在互聯網連接可用時更新應用程序。所以我們需要從在線服務器將一些圖像文件下載到本地文件系統中(iOS和Android)。 下面是示例中使用JavaScript代碼:Phonegap 3.5.0 Cordova File API錯誤:無法讀取未定義的屬性'dataDirectory'
try{
//The directory to store data
var store;
//Used for status updates
var $status;
//URL of our asset
var assetURL = "https://raw.githubusercontent.com/cfjedimaster/Cordova-Examples/master/readme.md";
//File name of our important data file we didn't ship with the app
var fileName = "mydatafile.txt";
//////////////////////
alert("Checking for data file.");
//Check for the file.
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
/////////////////////////////
} catch(e){
alert(e.message);
}
function downloadAsset() {
var fileTransfer = new FileTransfer();
alert("About to start transfer");
fileTransfer.download(assetURL, store + fileName,
function(entry) {
alert("Success!");
appStart();
},
function(err) {
alert("Error!");
console.dir(err);
alert(err);
});
}
//I'm only called when the file exists or has been downloaded.
function appStart() {
// $status.innerHTML = "App ready!";
alert("App ready!");
}
function onFileSystemSuccess() {
try{
store = cordova.file.dataDirectory;
//Check for the file.
window.resolveLocalFileSystemURL(store + fileName, appStart, downloadAsset);
} catch(e){
alert(e.message);
}
}
function onError(){
alert('error');
}
當啓動應用程序,結果是2個警報:
無法讀取屬性 'DataDirectory目錄' 未定義
// alert(e.message);
錯誤
// alert('Error');
是否在_deviceReady_被觸發後執行'window.requestFileSystem'? –
是的,它在deviceready事件被觸發後執行。 –
並且您已經爲該平臺正確安裝了文件插件?您是否可能使用PhoneGap Build,或者只是普通的Cordova CLI? –