Phonegap 3.5.0 Cordova File API错误:无法读取属性' dataDirectory'未定义的

时间:2015-01-14 10:22:21

标签: android ios file cordova

我们必须使用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个警报:

  • 无法读取未定义的属性'dataDirectory' // alert(e.message);

  • 错误// alert('Error');

2 个答案:

答案 0 :(得分:0)

好的,我们发现了问题! 在应用程序内部,我们使用下载的cordova插件版本包含了cordova js文件,该插件包含在应用程序的js文件夹中。 所以而不是下面的内容:

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

建议输入:

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

Phongap将自己添加cordova文件。

答案 1 :(得分:0)

这可能听起来很愚蠢,但对于所有类型的&#34;无法阅读财产&#34;问题,您也可以尝试

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

它解决了我根本没有意义的问题。谁会期望插件本身出错呢?