FileReader not firing onloadend in Ionic 2

烂漫一生 提交于 2020-01-03 15:50:13

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/36283445/filereader-not-firing-onloadend-in-ionic-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!