簡體   English   中英

Phonegap 3.5.0 Cordova File API錯誤:無法讀取未定義的屬性“ dataDirectory”

[英]Phonegap 3.5.0 Cordova File API Error: cannot read property 'dataDirectory' of undefined

我們必須使用PhoneGap Build和Apache Cordova為iOS和Android構建應用程序。 Phonegap版本是3.5.0。 並且我們想在有互聯網連接時更新應用程序。 因此,我們需要將一些圖像文件從在線服務器下載到本地文件系統中,再放入應用程序(iOS和Android)中。 這是使用的示例JavaScript代碼:

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條警報:

  • 無法讀取未定義 // alert(e.message);屬性“ dataDirectory” // alert(e.message);

  • 錯誤// alert('Error');

好的,我們發現了問題! 在應用程序內部,我們使用下載版本的cordova插件包含了cordova js文件,該文件包含在應用程序的js文件夾中。 因此,而不是以下內容:

<script type="text/javascript" src="js/cordova.js"></script> 

建議鍵入:

<script type="text/javascript" src="cordova.js"></script> 

並且Phongap將自己添加cordova文件。

這聽起來可能很愚蠢,但是對於各種“無法讀取屬性”的問題,您也可以嘗試

rm -rf plugins
cordova/ionic platform remove xxx
cordova/ionic platform add xxx

它解決了我根本沒有任何意義的問題。 誰會期望插件本身出錯?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM