Cordova检查现有文件

时间:2016-05-10 08:20:04

标签: cordova phonegap-plugins

使用Cordova 5.4.1和cordova-plugin-file 4.1.1

我想检查日志文件是否存在,如果不存在,请创建一个。问题是 - 每次我的应用程序启动时,它都会创建新的日志文件。不知何故,它无法看到它是否已经存在。我确实在所需的文件夹中看到了它。

我已将代码放在下面。请帮忙。问题区域"问题 - 即使文件存在,也不会调用此成功处理程序"在代码中。感谢。

var jsGStrLogFName = "MHALog.txt";

document.addEventListener('deviceready', onDeviceReady.bind(this), false);

//deviceready callback
function onDeviceReady() {
    //alert("Cordova deviceready called");
    console.log("Cordova deviceready called");

    //Handle the Cordova pause and resume events
    document.addEventListener('pause', onPause.bind(this), false);
    document.addEventListener('resume', onResume.bind(this), false);

    //05062016 - commented / written in favor of event based online offline check
    //Handle online/offline events
    document.addEventListener('online', onOnline.bind(this), false);
    document.addEventListener('offline', onOffline.bind(this), false);

    //backButton
    document.addEventListener("backbutton", onBackKeyDown.bind(this), false);
    //orientationChange
    window.addEventListener("orientationchange", function () {
        //resize or align
    });

    //TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.

    //logging code - initialize
    //does the file exist or not?
    //Issue - even though file exists, this success handler never gets called
    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory + jsGStrLogFName,
        function (lgFile) {
            console.log("Got the " + jsGStrLogFName + " file - it exists");
            jsGObjlogOb = lgFile;
            jsFnWriteMHALog("MHA - 1 deviceready fired and device events attached");
        },
        function () {
            console.log(jsGStrLogFName + " doesn't exist... need to create it");

            window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (dir) {
                dir.getFile(jsGStrLogFName, { create: true }, function (lgFile) {
                    console.log(jsGStrLogFName + " was created.");
                    jsGObjlogOb = lgFile;
                    jsFnWriteMHALog("MHA - 2 deviceready fired and device events attached");
                }, function () {
                    console.log("Can't create file:" + jsGStrLogFName);
                });
            });

        });
}

1 个答案:

答案 0 :(得分:0)

当我使用visual studio 2015和apache cordova工具调试代码时,我发现这种情况正在发生。当我部署它时,正在找到日志文件并附加到正好。