使用Cordova File Plugin读取文件

Mat*_*van 10 javascript android filereader cordova phonegap

我正在使用cordova文件插件 - cordova文件插件

我实际上是从文本文件中读取文件.以下是我的代码

document.addEventListener("deviceready", function () {  
    window.resolveLocalFileSystemURL(cordova.file.applicationDirectory + "sameple.txt", gotFile, fail);
}, true);

function gotFile(file) {           
    file.file(function (file) {                
        var reader = new FileReader();
        reader.onloadend = function (evt) {          
            console.log(this.result);
        }
        reader.readAsText(file);                
    }, fail());            
}

function fail(e) {
    console.info("FileSystem Error : " + e);
}
Run Code Online (Sandbox Code Playgroud)

因此每当我运行此代码时,我都会收到以下错误

deviceready has not fired after 5 seconds.
Channel not fired: onPluginsReady
Channel not fired: onCordovaReady

Could not get Cordova FileSystem: Error: deviceready has not fired after 5 seconds.
   "Could not get Cordova FileSystem:"
   {
      [functions]: ,
      __proto__: { },
      description: "deviceready has not fired after 5 seconds.",
      message: "deviceready has not fired after 5 seconds.",
      name: "Error"
   }

{"data":"data"}
Run Code Online (Sandbox Code Playgroud)

之后deviceready error我能够获得确切的数据..我该如何解决这个错误?我必须等待设备就绪执行完成吗?

dan*_*dan 5

它可能由于多种原因而发生,请参阅此处的更多详细信息.

你应该尝试一下大部分时间都适用的@bayanAbuawad建议:

  1. 添加带有cordova平台的平台添加ios android

  2. 用cordova平台删除它们删除ios android

  3. 再次添加它们.

这是非常奇怪的,但它与平台文件夹中的错误android.json和ios.json有关.