【问题标题】:Cordova 3.4 FileReader not working (no onloadend)Cordova 3.4 FileReader 不工作(没有 onloadend)
【发布时间】:2023-04-02 22:39:01
【问题描述】:

我正在将一个应用程序从 phonegap 2.* 更新到 cordova 3.4 现在一切顺利,只是文件下载不工作。

我需要从 Internet 下载一个文件(主机已编辑)并将其存储为 JSON 文件,以便稍后处理内容。 下载工作正常,文件将显示在文件系统中,但 FileReader 不会触发 onloadend 事件。

我尝试了一些事情,例如 onprogressonerror 事件,还有 file.toURIFileReader.readAsDataURL - 没有任何效果。有人有什么想法吗?

注意事项:

  • app.log 可以看作是console.log 的别名
  • print_r 在另一个文件中定义,工作正常
  • 下载的文件只有几 kB,应该不是性能问题
  • 在 iOS 硬件上运行

完整代码(提取和缩短):

var fileTransfer = new FileTransfer();

var loadingStatus = 0;
fileTransfer.onprogress = function (progressEvent) {

    // if we have the complete length we can calculate the percentage, otherwise just count up
    if (progressEvent.lengthComputable) {
        loadingStatus = Math.floor(progressEvent.loaded / progressEvent.total * 100);
    } else {
        loadingStatus++;
    }
    app.log('Transfer Progress: ' + loadingStatus);

};

fileTransfer.download(
    encodeURI('http://www.example.com/export'),
    'cdvfile://localhost/persistent/import.json',
    function (file) {

        var FileReader = new FileReader();

        FileReader.onloadend = function (evt) {
            app.log('Filereader onloadend');
            app.log(evt);
        };

        FileReader.readAsText(file);

    },
    function (error) {

        // FileTransfer failed
        app.log("FileTransfer Error: " + print_r(error));

    }
);

【问题讨论】:

    标签: javascript ios cordova filereader


    【解决方案1】:

    文件 API 已更新。看到这个帖子:https://groups.google.com/forum/#!topic/phonegap/GKoTOSqD2kc

    file.file(function(e) {
        console.log("called the file func on the file ob");
        var reader = new FileReader();
        reader.onloadend = function(evt) {
            app.log('onloadend');
            app.log(evt.target.result);
        };
        reader.readAsText(e);
    
    });
    

    【讨论】:

      【解决方案2】:

      目前无法验证这一点,但自 3.0 起,Cordova 将设备级 API 实现为插件。使用命令行界面中描述的 CLI 的插件命令为项目添加或删除此功能:

      $ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-file.git
      $ cordova plugin rm org.apache.cordova.core.file
      

      您是否将插件添加到您的项目中?

      【讨论】:

      • 当然所有插件都已添加并运行,否则无法下载 ;-)
      • 嗯,你检查过 onloadstart 和 onload 事件是否触发了吗?
      • 事件没有触发,似乎没有来自 Phillip 的更改,readAsText 函数根本没有被调用。遗憾的是,JavaScript 或 Cordova 没有发出任何通知。
      猜你喜欢
      • 1970-01-01
      • 2016-07-16
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      • 2015-05-20
      • 2017-05-28
      • 2018-02-15
      • 2022-10-17
      相关资源
      最近更新 更多