javascript - FileReader not firing onloadend in Ionic 2 - Stack Overflow

I'm trying to read a local file using the cordova-file-plugin. At the moment I'm able to read

I'm trying to read a local file using the cordova-file-plugin. At the moment I'm able to read the content of local directories and select a single file. But I have problems to get the content of the file.

Here's my function that's called by clicking a button after selecting a file from list:

import() {
    window.resolveLocalFileSystemURL(this.file.nativeURL, this.gotFile, this.fail);
}

And here're the two functions gotFile and fail:

fail(e) {
    console.log(e);
}

gotFile(fileEntry) {
    var file = fileEntry.nativeURL;

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

        reader.onloadend = function(e) {
             console.log('onloadend()'); 
        };

        reader.readAsText(file);

        console.log(reader);
    }); 
}

I can see in the log the reader with all the wanted content of my file under result, but I'm not able to get it and use it. The readyState is 2, but all inside reader.onloadend isn't called. With Ionic 1 this code works without any problems.

I would be very glad, if there's anyone how can help me. Thanks in advance!

I'm trying to read a local file using the cordova-file-plugin. At the moment I'm able to read the content of local directories and select a single file. But I have problems to get the content of the file.

Here's my function that's called by clicking a button after selecting a file from list:

import() {
    window.resolveLocalFileSystemURL(this.file.nativeURL, this.gotFile, this.fail);
}

And here're the two functions gotFile and fail:

fail(e) {
    console.log(e);
}

gotFile(fileEntry) {
    var file = fileEntry.nativeURL;

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

        reader.onloadend = function(e) {
             console.log('onloadend()'); 
        };

        reader.readAsText(file);

        console.log(reader);
    }); 
}

I can see in the log the reader with all the wanted content of my file under result, but I'm not able to get it and use it. The readyState is 2, but all inside reader.onloadend isn't called. With Ionic 1 this code works without any problems.

I would be very glad, if there's anyone how can help me. Thanks in advance!

Share Improve this question asked Mar 29, 2016 at 11:49 user2836375user2836375 1392 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

Known issue in ionic 2

Loading zone.js before cordova.js fixes it.

Specific to ionic2 maintain this sequence order.

<script src="build/polyfills.js"></script> <script src="cordova.js"</script>

as the polyfills.js executes the zone.js.

Although I haven't used onloadend, I've been successfully using the onload event handler instead in Ionic 2, maybe you could give that one a try.

Keep in mind that it will return only if the operation is successful.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743876667a4522422.html

相关推荐

  • javascript - FileReader not firing onloadend in Ionic 2 - Stack Overflow

    I'm trying to read a local file using the cordova-file-plugin. At the moment I'm able to read

    3天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信