Cordova 3.4 FileReader 不工作(没有 onloadend)

Posted

技术标签:

【中文标题】Cordova 3.4 FileReader 不工作(没有 onloadend)【英文标题】:Cordova 3.4 FileReader not working (no onloadend) 【发布时间】:2014-05-13 07:07:27 【问题描述】:

我正在将一个应用程序从 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));

    
);

【问题讨论】:

【参考方案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 没有发出任何通知。

以上是关于Cordova 3.4 FileReader 不工作(没有 onloadend)的主要内容,如果未能解决你的问题,请参考以下文章