Cordoba 3.4 FileReader not working (no onloadend)

I am updating the application from the telephone plug 2. * to cordova 3.4 Now everything is working fine, only downloading files does not work.

I need to download a file from the Internet (edited by the host) and save it as a JSON file so that the content can be processed later. The download works fine, the file will be displayed on the file system, but FileReader does not fire an event onloadend.

I tried a few things like the events onprogressor onerror, as file.toURIand FileReader.readAsDataURL- nothing worked. Anyone any ideas?

Notes:

  • app.log can be considered as an alias for console.log
  • print_r defined in another file, works great
  • The downloaded file is only a few kilobytes, it should not be a performance problem
  • Work on iOS equipment

Full code (extracted and abbreviated):

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));

    }
);
+4
source share
2 answers

Updated API files. See this post: 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);

});
+5
source

I cannot verify this at the moment, but starting with 3.0, Cordova implements device-level APIs as plugins. Use the CLI plugin command described in the command line interface to add or remove this function for the project:

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

Have you added the plugin to your project?

0
source

Source: https://habr.com/ru/post/1540326/


All Articles