Cordova resolveLocalFileSystemURL callbacks not fi

2019-07-31 07:48发布

I've been trying to run the following code but the callbacks [ok() and ko()] are not called.
Using Worklight 6.2 (Cordova 3.4).

function wlCommonInit() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
            success, fail);

    window.resolveLocalFileSystemURL(cordova.file.applicationDirectory
            + "www/index.html", ok, ko);
}

function ko(e) {
    alert("NO");
}

function ok(fileEntry) {
    alert("OK");
}

On the other hand requestFileSystem callbacks are called regularly.

2条回答
在下西门庆
2楼-- · 2019-07-31 08:29

The code snippet in the question will not work in Android due to a Cordova defect: https://issues.apache.org/jira/browse/CB-7273.

To progress further, it would help to understand what are your plans for the file itself.

  • Do you simply want the path to the file
  • Or do you want to alter the contents of the file?
  • Or?

You can read more about file system operations in Cordova in this question/answer: Where does LocalFileSystem.PERSISTENT point to?

查看更多
走好不送
3楼-- · 2019-07-31 08:37

I managed to access local fiiles in Worklight running an Android environment using a XMLHttpRequest:

//Works only on Android
function prendiCaricaRisorsaWorklight(percorsoPiuNomeFile) {

    var xmlhttp = new XMLHttpRequest();
    var risposta = "";
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4
                && (xmlhttp.status == 200 || xmlhttp.status == 0)) {
            risposta = xmlhttp.responseText; 
            alert(risposta); 
        }
    }
    xmlhttp.open("GET", "file:///android_asset/www/default/"
            + percorsoPiuNomeFile, true);
    xmlhttp.send(null);
}

Usage example:

prendiCaricaRisorsaWorklight("css/cssInterno.css");
prendiCaricaRisorsaWorklight("js/jsInterno.js");

This shows on Android an alert with the file content.

查看更多
登录 后发表回答