question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FileReader API not firing

See original GitHub issue

Description of the problem: In my project I’m trying to read and convert a Blob downloaded from the dropbox api to a text string. When building my project using Cordova, the FileReader fires onload and onprogress events. When building using Capacitor, filereader does not fire any events at all.

Affected platform

  • Android
  • [x ] iOS
  • electron
  • web

OS of the development machine

  • Windows
  • [ x] macOS
  • linux

Other information: As suggested in other bug reports, I tried wrapping the FileReader in

platform.ready().then(_ => {
/// perform file reading 
})

and in

zone.run(() => {
/// perform file reading
})

None helped. Cordova has no issues reading the file.

When I use

var text = await (new Response(response.fileBlob)).text();

to read the file blob, it does work.

Capacitor version: 1.0.0-beta.24

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:42 (6 by maintainers)

github_iconTop GitHub Comments

164reactions
tripodsgamescommented, Jun 29, 2021

You can try this.

export function getFileReader(): FileReader {
    const fileReader = new FileReader();
    const zoneOriginalInstance = (fileReader as any)["__zone_symbol__originalInstance"];
    return zoneOriginalInstance || fileReader;
}
...
let newInstance = getFileReader();
25reactions
moridianmesscommented, Aug 11, 2020

If you have plugins that rely on FileReader working correctly, using snippets from the getFileReader() code mentioned above, I have a solution

Add to app.module.ts:

export class FileReaderA extends window.FileReader {
	constructor() {
		super();
		const zoneOriginalInstance = (this as any)['__zone_symbol__originalInstance'];
		return zoneOriginalInstance || this;
	}
}

window.FileReader = FileReaderA;

or add the above code to another file and import the file, not the class.

This is a hack but it means that when calling new FileReader() all the events work. Ideally it would be nice to be able to control the order in which scripts are imported. As the fix I’ve always used in a cordova based project is to change the order of polyfills.js and cordova.js in the index.html file.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FileReader onload function is not firing - Stack Overflow
This means that you are not dealing with the web-API's FileReader, but rather with some kind of a wrapper object.
Read more >
FileReader: error event - Web APIs | MDN
The error event is fired when the read failed due to an error (for example, because the file was not found or not...
Read more >
File readAsText not working - Capacitor - Ionic Forum
I tried to use the file-plugin like described here: https://ionicframework.com/docs/native/file Writing to a file seems to work.
Read more >
File API - W3C
Publication as a Working Draft does not imply endorsement by W3C and its ... 6.1 The File Reading Task Source; 6.2 The FileReader...
Read more >
FileReader onload not working in lightning component
You didn't actually read the file. You need to call one of readAsArrayBuffer, readAsBinaryString, readAsDataURL, or readAsText.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found