filePluginIsReady event is never fired in chrome w

2019-07-04 23:53发布

问题:

I am now developing a cordova app whitch platform is browser(Chrome).

I failed when using the cordova-plugin-file to read a file.

According to the document of cordova-plugin-file : Chrome quirks, It said that:

Chrome filesystem is not immediately ready after device ready event. As a workaround you can subscribe to filePluginIsReady event....You can use window.isFilePluginReadyRaised function to check whether event was already raised.

I wrote my code like this :

document.addEventListener('deviceready', dataRead, false);
function dataRead() {
  window.addEventListener('filePluginIsReady', readyToRead, false);
  console.log(window.isFilePluginReadyRaised());
}

function readyToRead(){
  window.initPersistentFileSystem(10*1024*1024, function() {
    var fs = cordova.file.applicationDirectory;
    console.log(fs);
    window.resolveLocalFileSystemURL(cordova.file.applicationDirectory + "www/1111.csv", gotFile, fail);
  },function (e) {
    console.log(e);
  });
}

function fail(e) {
  console.log("FileSystem Error");
  console.dir(e);
}

function gotFile(fileEntry) {
  fileEntry.file(function(file) {
    var reader = new FileReader();

    reader.onloadend = function(e) {
      console.log("Text is: "+this.result);
    }
    reader.readAsText(file);
  });
}

It failed to read the file, and the message in console likes this:

adding proxy for File ------------------cordova.js:942

Persistent fs quota granted ---------Preparing.js:170

false --------------------------------------app.js:4 (value of window.isFilePluginReadyRaised())

It seems that filePluginIsReady event didn't fired! WHY?

Besides, if I write my code under the deviceready event directly. It will also fail with error message below:

code: 5

message: "A URI supplied to the API was malformed, or the resulting Data URL has exceeded the URL length limitations for Data URLs."

name: "EncodingError"

Can any one point out why or show me a right example?

Any help is appreciated.

回答1:

one thing why this could have happened is that probably the time you attached to the filePluginIsReady the event was already fired. Try to subscribe to the event in the appStart.js or the first file in your app. It worked for me