【问题标题】:Phonegap 3.5.0 Cordova File API Error: cannot read property 'dataDirectory' of undefinedPhonegap 3.5.0 Cordova 文件 API 错误:无法读取未定义的属性“dataDirectory”
【发布时间】:2015-03-12 12:01:02
【问题描述】:

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

  • 无法读取 undefined 的属性“dataDirectory”// alert(e.message);

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

【问题讨论】:

  • window.requestFileSystem 是否在 deviceReady 被触发后执行?
  • 是的,它在 deviceready 事件触发后执行。
  • 您是否为该平台正确安装了文件插件?您可能使用的是 PhoneGap Build,还是只使用普通的 Cordova CLI?
  • Phonegap Build 的 config.xml 文件中使用了 File 插件。

标签: android ios file cordova


【解决方案1】:

好的,我们找到了问题! 在应用程序内部,我们使用下载的cordova插件版本包含了cordova js文件,该文件包含在应用程序的js文件夹中。 所以代替下面的:

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

建议输入:

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

Phongap 将自己添加cordova 文件。

【讨论】:

    【解决方案2】:

    这听起来可能很愚蠢,但对于各种“无法读取属性”的问题,您也可以尝试

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

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

    【讨论】:

    • 插件有时会出现需要修复的错误,这个问题在许多环境中都会发生......
    猜你喜欢
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    相关资源
    最近更新 更多