Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file reader plugin events are not firing #505

Closed
jgw96 opened this issue Aug 29, 2016 · 56 comments
Closed

file reader plugin events are not firing #505

jgw96 opened this issue Aug 29, 2016 · 56 comments

Comments

@jgw96
Copy link

jgw96 commented Aug 29, 2016

From @comfortme on August 27, 2016 16:43

Short description of the problem:

while using file plugin with ionic-native found out that readAsArrayBuffer(path, file) promise never resolves!
i did some digging and turns out it's an fixed angular 2 zone issue and here is a detailed closed ionic issue about it. ionic-team/ionic-framework#6020
it looks like it's never been fixed for ionic 2

What behavior are you expecting?

file promises to be resolved

Steps to reproduce:

  1. setup a new project
  2. install camera and file plugin
  3. getpicture with camera and try to read it with readAsArrayBuffer
Camera.getPicture(options).then((imageData) => {
File.readAsArrayBuffer(imageData).then((res)=>{
//never resolves.
});
});

Other information: (e.g. stacktraces, related issues, suggestions how to fix, stackoverflow links, forum links, etc)
angular/zone.js#137
angular/angular#2533

Which Ionic Version? 1.x or 2.x
Cordova CLI: 6.0.0
Ionic Framework Version: 2.0.0-beta.11
Ionic CLI Version: 2.0.0-beta.36
Ionic App Lib Version: 2.0.0-beta.19
OS: Windows 8.1
Node Version: v4.4.3

Copied from original issue: ionic-team/ionic-framework#7891

@stalniy
Copy link
Contributor

stalniy commented Aug 30, 2016

Currently the only possible workaround is:

const WrappedFileReader = window.FileReader
window.FileReader = function OriginalFileReader(...args) {
  WrappedFileReader.apply(this, args)
  const originalInstance = this[Zone.__symbol__('originalInstance')] // eslint-disable-line

  return originalInstance || this
}

@vknabel
Copy link

vknabel commented Sep 8, 2016

I have had this issue, too. After updating from 1.3.17 to 1.3.21 everything worked again.

@ihadeed
Copy link
Collaborator

ihadeed commented Oct 12, 2016

@comfortme Can you try with the latest version v2.2.2 and report back if you still have any issue? Thanks.

@ihadeed ihadeed closed this as completed Oct 12, 2016
@captaincole
Copy link
Contributor

captaincole commented Oct 20, 2016

@ihadeed I am still seeing this issue for ios only. I am using zone.js v0.6.25 at the moment, with ionic-angular v2.0.0-rc.1 and ionic-native v2.2.3

I tried the workaround and the events are now firing on ios, but then I recieve and have to workaround this error on android.

Uncaught TypeError: i.readAsArrayBuffer is not a function

This is still an open issue for ionic-native.

@pdrosos
Copy link

pdrosos commented Dec 6, 2016

I also have this issue with ionic-native 2.2.10 and strange is that it doesn't happen always and it's very hard to tell how and why exactly does it happen. When I modify other parts of my code and build the app, File.readAsArrayBuffer sometimes just stops working and its promise never resolves or rejects.

@ihadeed
Copy link
Collaborator

ihadeed commented Dec 7, 2016

@pdrosos Please create a new issue with steps to reproduce this. Also, make sure you're waiting for deviceready event to fire or wrapping your code with this.platform.ready().then(() => { ... }).

@mmazloum
Copy link

mmazloum commented Jan 5, 2017

@ihadeed @pdrosos

I still need the workarround suggested by @stalniy to manage my photos loading on ios and android, but it is strange since it works perfectly on ios emulator without the workarround.

@ihadeed ihadeed reopened this Jan 7, 2017
@ihadeed
Copy link
Collaborator

ihadeed commented Jan 7, 2017

@comfortme @mmazloum

Sorry I just had another look at the original issue here.

You are having problems because you are passing imageData directly to readAsArrayBuffer. You need to split the file URI into path and file name, then pass those to the readAsArrayBuffer method.

Here is an example: (or here)

        Camera.getPicture()
          .then(imageData => {
            const fileName = imageData.split('/').pop();
            const path = imageData.replace(fileName, '');
            return File.readAsArrayBuffer(path, fileName);
          })
          .then(response => {
            console.log('File response is: ', response);
          })
          .catch(console.error.bind(console));

@ihadeed ihadeed closed this as completed Jan 7, 2017
@dbpieter
Copy link

@ihadeed

We are still seeing this issue.
The fileReader.onloadend callback never fires.

We are using

  • @angular 2.4.1 (with ng-upgrade)
  • angular 1.5.8
  • zone.js 0.7.2
  • cordova plugin file 4.3.0
  • ionic.native 2.2.14

@ihadeed
Copy link
Collaborator

ihadeed commented Jan 10, 2017

@dbpieter can you share the code so I can have a look?

@mmazloum
Copy link

@ihadeed the last solution you suggested worked on ios, but the promise still doesn't resolve on android.

here is a code sample :

  //             const fileName2 = fileUri.split('/').pop();
  //             const path = fileUri.replace(fileName2, '');
  //             console.log('fileName2: ', fileName2);
  //             console.log('path: ', path);
  //             File.readAsDataURL(path, fileName2).then((res) => {
  //               //console.log(res);
  //               //let base64img = res.target.result;
  //               this.imgSrc=res as string;
  //               this.changeDetector.detectChanges();
  //               console.log("image base64 loaded");
  //               this.uploading = true;
  //               let blob = this.dataURItoBlob(this.imgSrc);

@dbpieter
Copy link

dbpieter commented Jan 10, 2017

@ihadeed
my code is similar to @mmazloum 's code. The issue only occurs on Android.
Loading zone.js before cordova.js also fixes it.

There seems to be a problem with the patching of FileReader (both zone and cordova-plugin-file do this)
It appears the reader.onloadend callback gets never executed.

@joewoodhouse
Copy link

I currently have this problem with:

  • angular 2.2.1
  • ionic 2.0.0-rc4
  • ionic-native 2.2.11
  • cordova plugin file 4.3.0
  • zone.js 0.6.26

I can't get any of the workarounds to work. @dbpieter what do you mean by "Loading zone.js before cordova.js also fixes it." and if that is a fix, could you give some more details?

My app is currently completely dead in the water because of this :(

@dbpieter
Copy link

dbpieter commented Jan 11, 2017

@joewoodhouse
The zone.js dist script must be run before the cordova.js file so your index.html eventually look like this (whatever buildsystem you are using)

  • <script src="core.js></script>
  • <script src="zone.js"></script> (zone js needs es6 polyfill)
  • <script src="cordova.js></script>
  • <script src="otherstuffineed.js></script>

The workaround suggested by @stalniy also works for us. However this seems like a slightly cleaner fix.

@joewoodhouse
Copy link

Hmm I'm using a pretty standard ionic2 app setup, and I have no zone.js script - is it not bundled up into the main.js?

@dbpieter
Copy link

@joewoodhouse It's probably bundled into your main.js yes (which is the problem). We created a separate bundle with zone and core that gets loaded before cordova.js. (we use webpack as our build system). Our main.js (excluding core and zone) gets loaded after cordova.js

@stalniy
Copy link
Contributor

stalniy commented Jan 12, 2017

@dbpieter I'm not sure but this is my vision of how it works when you put zonejs before cordovajs:

  • zone is loaded and trying to patch FileReader
  • html5 FileReader is patched if found
  • Cordova-file-plugin is initialized (by cordovajs)
  • Cordova-fileplugin overwrites html5 FileReader
  • we have zone free FileReader provided by Cordova-file-plugin

In this case it works as you want but there may be issues with other plugins and zonejs

@BjornRombaut
Copy link

@stalniy does the cordova-file-plugin overwrite the HTML5 FileReader or is it patched? I thought it would work as following:

On Load

  • zone.js patches the HTML5 FileReader
  • cordova-file-plugin patches the already patched by zone.js FileReader

On Execute

  • FileReader executes cordova-file-plugin code
  • FileReader executes zone.js code
  • FileReader executes native code

@joewoodhouse
Copy link

I had a bit more success with this by wrapping everything in a this.platform.ready().then(()=>{}). It now seems to reliably call the onloadend callback.

However I'm not really sure why I need to do this. My use of FileReader is not anywhere near to app startup time so there should be no issue with the platform being ready or not. Perhaps this.platform.ready actually does something more complicated than the documentation eludes to? From looking at the source NgZone does seem to be involved, so perhaps that is the issue/solution?

@mmazloum
Copy link

@joewoodhouse I did this to and kept on having the problem, but I remember it looked solved on the first trials because I hadn't rebuilt my android app properly.

@stalniy
Copy link
Contributor

stalniy commented Jan 12, 2017

@BjornRombaut as you can see from source https://github.com/apache/cordova-plugin-file/blob/master/www/FileReader.js it actually overwrites (or maybe better say wraps FileReader) and as a result FileReader api is not even called everytime. For local files cordova-file-plugin uses different strategy (https://github.com/apache/cordova-plugin-file/blob/master/www/FileReader.js#L268) and maybe this is the root cause of integration issue between cordova-file-plugin and zone.js.

@SwapnilKale747
Copy link

SwapnilKale747 commented Jan 13, 2017

Here is the working example for reading image in base64 format,
residing in device using FILE (FILE.readAsDataURL) ..
(https://github.com/apache/cordova-plugin-file) (From documentation) ..
I hope It helps..
@ihadeed @jgw96

                       let fileName = path.split('/').pop();
                       let filePath = path.replace(fileName, '');;

                      fileName = fileName.split('?')[0];   // --> This Line is added to make it work in case of Image...
                       File.readAsDataURL(filePath, fileName).then((data) => {
                         this.final_Image = data;
                       }).catch(err => { });

@jineshmehta16
Copy link

jineshmehta16 commented Jun 29, 2018

Is there anyone who has reorder the polyfill.js before cordova.js still doesn't get the solution? If yes can you please respond what steps did you do to solve the problem

Also if by reordering if you are able to resolve can you please paste here your version of package.json

@Domvel
Copy link
Contributor

Domvel commented Jan 24, 2019

It’s a zone.js issue. To fix it, edit the index.html and move the cordova.js (from head) after polyfills.js (body) like

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

Cordova must included AFTER Polyfills

But is there an official solution?

"@angular/core": "5.2.10",
"ionic-angular": "3.9.2",
"ionic": "^4.2.1",
"zone.js": "0.8.26"

Update: The placement of cordova after polyfills will produce new problem in prod build. Maybe load cordova twice (head and body after polyfills)? 😄

@Domvel
Copy link
Contributor

Domvel commented Jan 25, 2019

How can this problem be 100% reproduced? Does anyone have an idea? It's hard to test it with this sporadic behavior.

I'm currently testing the solution of @stalniy I placed it in index.html on bottom after all other scripts. Thanks for this solution. But I'm not yet sure if this works. Because this poor reproducibility of this bug.

@Domvel
Copy link
Contributor

Domvel commented Jan 29, 2019

@stalniy Your solution with WrappedFileReader does not work for me. I get the error fileReader.readAsArrayBuffer is not a function.

@KnowYourselves
Copy link

Still getting this error and none of the solutions work.

@dansterrett
Copy link

dansterrett commented Jun 18, 2019

I was able to fix my issue by getting the original native browser instance and using it.

let fileReader = new FileReader();

// Get the original real FileReader. The polyfill saves a reference to it.
const realFileReader = fileReader._realReader;

// Make sure we were able to get the original FileReader 
if (realFileReader) {
    // Swap out the polyfill instance for the original instance.
    fileReader = realFileReader;
}

fileReader.readAsArrayBuffer(someBlob);

This worked fine in Android and iOS.

---------------EDIT-----------------
Since I've posted the above code, I've had to make one small fix. If the file you are reading is not a real file (i.e. not a Blob), then you don't want to use the real FileReader).

Here's a more detailed example:

getBlobFromFile(file: File): Promise<Blob> {
    return new Promise((resolve, reject) => {

      let fileReader = new FileReader();

      // Is this a "real" file? In other words, is this an instance of the original `File` class (not the one overriden by cordova-plugin-file).
      // If so, then we need to use the "real" FileReader (not the one overriden by cordova-plugin-file).
      if (file instanceof Blob) {
        const realFileReader = (fileReader as any)._realReader;
        if (realFileReader) {
          fileReader = realFileReader;
        }
      }

      fileReader.onloadend = function() {
        resolve(new Blob([this.result], { type: file.type }));
      };

      fileReader.onerror = function(err) {
        console.error('Inside fileReader.onerror', err);
        reject(err);
      };

      fileReader.readAsArrayBuffer(file);
    });
  }

@lauramzarescu
Copy link

lauramzarescu commented Jun 26, 2019

I was able to fix my issue by getting the original native browser instance and using it.

let fileReader = new FileReader();

// Get the original real FileReader. The polyfill saves a reference to it.
const realFileReader = fileReader._realReader;

// Make sure we were able to get the original FileReader 
if (realFileReader) {
    // Swap out the polyfill instance for the original instance.
    fileReader = realFileReader;
}

fileReader.readAsArrayBuffer(someBlob);

This worked fine in Android and iOS.

I got this error when trying to use your solution: "Property '_realReader' does not exist on type 'FileReader'". Any advice?

@dansterrett
Copy link

That looks like a Typescript error, not a JavaScript error. If so, you just need to cast the fileReader to the any type. Something like this should work.

const realFileReader = (fileReader as any)._realReader;

@mixuala
Copy link

mixuala commented Aug 6, 2019

I was able to fix my issue by getting the original native browser instance and using it.

let fileReader = new FileReader();

// Get the original real FileReader. The polyfill saves a reference to it.
const realFileReader = fileReader._realReader;

// Make sure we were able to get the original FileReader 
if (realFileReader) {
    // Swap out the polyfill instance for the original instance.
    fileReader = realFileReader;
}

fileReader.readAsArrayBuffer(someBlob);

This worked fine in Android and iOS.

This also worked for me, but make sure you attach your onloadend event handler to realFileReader and not @Ionic-native FileReader.

@hnguyen48206
Copy link

hnguyen48206 commented Oct 31, 2019

I was able to fix my issue by getting the original native browser instance and using it.

let fileReader = new FileReader();

// Get the original real FileReader. The polyfill saves a reference to it.
const realFileReader = fileReader._realReader;

// Make sure we were able to get the original FileReader 
if (realFileReader) {
    // Swap out the polyfill instance for the original instance.
    fileReader = realFileReader;
}

fileReader.readAsArrayBuffer(someBlob);

This worked fine in Android and iOS.

Hi, I'm having this exact issue (only on crosswalk, not default webview) and though I have been able to follow your instruction and successfully called the readAsArrayBuffer function, there is another the problem at the very last stage which is the input for it. "Uncaught TypeError: Failed to execute 'readAsArrayBuffer' on 'FileReader': parameter 1 is not of type 'Blob'"

Below is my piece of code and I'm pretty sure that the variable I passed as the parameter is indeed a File object and as you can see, I tried to cast it to File again right before passing it but there's no use.

  private readnewImgFile(file:any, index) {

   let reader = new FileReader();
    const realFileReader = (reader as any)._realReader;
    if (realFileReader) {
      console.log('has real file reader')
      console.log(file.type)
      realFileReader.onloadend = () => {
      //doing some stuff here with the result
      };
      realFileReader.onerror = function (error) {
        console.log(error);
        console.log('reader fail')
      };
      realFileReader.readAsArrayBuffer(<File>file);
    }
    else{
      console.log('no real filereader')
    }
}

When I console.log the file, these are the properties that I got

end:498212
lastModified:1572514141000
lastModifiedDate:1572514141000
localURL:"cdvfile://localhost/sdcard/Pictures/_img201993116291.png"
name:"_img201993116291.png"
size:498212
start:0
type:"image/png"

@dansterrett
Copy link

@hnguyen48206, since I've posted my original fix, I've had to alter the code a little bit. Here's the latest:

getBlobFromFile(file: File): Promise<Blob> {
    return new Promise((resolve, reject) => {

      let fileReader = new FileReader();

      // Is this a "real" file? In other words, is this an instance of the original `File` class (not the one overriden by cordova-plugin-file).
      // If so, then we need to use the "real" FileReader (not the one overriden by cordova-plugin-file).
      if (file instanceof Blob) {
        const realFileReader = (fileReader as any)._realReader;
        if (realFileReader) {
          fileReader = realFileReader;
        }
      }

      fileReader.onloadend = function() {
        resolve(new Blob([this.result], { type: file.type }));
      };

      fileReader.onerror = function(err) {
        console.error('Inside fileReader.onerror', err);
        reject(err);
      };

      fileReader.readAsArrayBuffer(file);
    });
  }

The main thing is you have to check if the file is a Blob or not. If the file is a Blob, then use the real FileReader. Otherwise, don't.

@hnguyen48206
Copy link

hnguyen48206 commented Nov 5, 2019

@hnguyen48206, since I've posted my original fix, I've had to alter the code a little bit. Here's the latest:

getBlobFromFile(file: File): Promise<Blob> {
    return new Promise((resolve, reject) => {

      let fileReader = new FileReader();

      // Is this a "real" file? In other words, is this an instance of the original `File` class (not the one overriden by cordova-plugin-file).
      // If so, then we need to use the "real" FileReader (not the one overriden by cordova-plugin-file).
      if (file instanceof Blob) {
        const realFileReader = (fileReader as any)._realReader;
        if (realFileReader) {
          fileReader = realFileReader;
        }
      }

      fileReader.onloadend = function() {
        resolve(new Blob([this.result], { type: file.type }));
      };

      fileReader.onerror = function(err) {
        console.error('Inside fileReader.onerror', err);
        reject(err);
      };

      fileReader.readAsArrayBuffer(file);
    });
  }

The main thing is you have to check if the file is a Blob or not. If the file is a Blob, then use the real FileReader. Otherwise, don't.

Thanks for your quick response, by checking the type of Blob, now I can assure that the 'file' returned from the FileEntry.file() is not an actual Blob/File indeed. However, I dont really know exactly what was returned cause there is no way to check what the 'file' is. If I console.log the FileEntry, it does have all the properties that listed here: https://cordova.apache.org/docs/en/1.9.0/cordova/file/fileentry/fileentry.html and should be legit. Then now the question is what is the thing that FileEntry.file() return? Any help would be much appriciated.

Below is the code I use to turn File URI to File entry before converting it to File/Blob
(*) variable pic is the uri of a picture (either taken by fresh capturing or picking from library)

  if (pic.startsWith("file")) {
        this.file
          .resolveLocalFilesystemUrl(pic)
          .then(entry => {
            console.log(entry);
            (entry as FileEntry).file(
              file=>{
                console.log(typeof file)
                console.log(file instanceof Blob)
                this.readnewImgFile(file, i)
              }, err=>{
                console.log(err)
              }
             );
          })
          .catch(err => console.log(err));
      } else {
        this.file
          .resolveLocalFilesystemUrl("file://" + pic)
          .then(entry => {
            console.log(entry);
            (entry as FileEntry).file(file =>
              {
                console.log(typeof file);
                console.log(file instanceof Blob);
                this.readnewImgFile(file, i);
              });
          })
          .catch(err => console.log(err));
      }

@dansterrett
Copy link

@hnguyen48206, that file object is defined here: https://github.com/apache/cordova-plugin-file/blob/master/www/File.js. In that same directory you can see the definitions for FileEntry and other applicable classes.

@joaopaschoal
Copy link

Im having trouble reading this file returned by "fileEntry.file(file =>...)" using the _realReader. It throws an error saying the "file" its not an actual Blob. How can I convert this "cordova file" into a native js Blob?
I tryied to use its file.slice() method, but it actually returns a new instance of file (the cordova one) instead of a blob.
Has anyone managed to convert this? Thanks in advance!

@felipecaparelli
Copy link

I was able to fix my issue by getting the original native browser instance and using it.

let fileReader = new FileReader();

// Get the original real FileReader. The polyfill saves a reference to it.
const realFileReader = fileReader._realReader;

// Make sure we were able to get the original FileReader 
if (realFileReader) {
    // Swap out the polyfill instance for the original instance.
    fileReader = realFileReader;
}

fileReader.readAsArrayBuffer(someBlob);

This worked fine in Android and iOS.

---------------EDIT-----------------
Since I've posted the above code, I've had to make one small fix. If the file you are reading is not a real file (i.e. not a Blob), then you don't want to use the real FileReader).

Here's a more detailed example:

getBlobFromFile(file: File): Promise<Blob> {
    return new Promise((resolve, reject) => {

      let fileReader = new FileReader();

      // Is this a "real" file? In other words, is this an instance of the original `File` class (not the one overriden by cordova-plugin-file).
      // If so, then we need to use the "real" FileReader (not the one overriden by cordova-plugin-file).
      if (file instanceof Blob) {
        const realFileReader = (fileReader as any)._realReader;
        if (realFileReader) {
          fileReader = realFileReader;
        }
      }

      fileReader.onloadend = function() {
        resolve(new Blob([this.result], { type: file.type }));
      };

      fileReader.onerror = function(err) {
        console.error('Inside fileReader.onerror', err);
        reject(err);
      };

      fileReader.readAsArrayBuffer(file);
    });
  }

Thank you so much. This solution worked for me with Ionic 5!

mjsorensen added a commit to briostack/ngx-pica that referenced this issue Aug 20, 2020
@fabriciovasc
Copy link

I was able to fix my issue by getting the original native browser instance and using it.

let fileReader = new FileReader();

// Get the original real FileReader. The polyfill saves a reference to it.
const realFileReader = fileReader._realReader;

// Make sure we were able to get the original FileReader 
if (realFileReader) {
    // Swap out the polyfill instance for the original instance.
    fileReader = realFileReader;
}

fileReader.readAsArrayBuffer(someBlob);

This worked fine in Android and iOS.

---------------EDIT-----------------
Since I've posted the above code, I've had to make one small fix. If the file you are reading is not a real file (i.e. not a Blob), then you don't want to use the real FileReader).

Here's a more detailed example:

getBlobFromFile(file: File): Promise<Blob> {
    return new Promise((resolve, reject) => {

      let fileReader = new FileReader();

      // Is this a "real" file? In other words, is this an instance of the original `File` class (not the one overriden by cordova-plugin-file).
      // If so, then we need to use the "real" FileReader (not the one overriden by cordova-plugin-file).
      if (file instanceof Blob) {
        const realFileReader = (fileReader as any)._realReader;
        if (realFileReader) {
          fileReader = realFileReader;
        }
      }

      fileReader.onloadend = function() {
        resolve(new Blob([this.result], { type: file.type }));
      };

      fileReader.onerror = function(err) {
        console.error('Inside fileReader.onerror', err);
        reject(err);
      };

      fileReader.readAsArrayBuffer(file);
    });
  }

was having problems when building for android, for ios devices, it worked normally.

both solutions, changing the order of cordova.js or instantiating the browser file reader worked.

Brazil thanks you! 🎉

@mwmw7
Copy link

mwmw7 commented Sep 25, 2020

@hnguyen48206, since I've posted my original fix, I've had to alter the code a little bit. Here's the latest:

getBlobFromFile(file: File): Promise<Blob> {
    return new Promise((resolve, reject) => {

      let fileReader = new FileReader();

      // Is this a "real" file? In other words, is this an instance of the original `File` class (not the one overriden by cordova-plugin-file).
      // If so, then we need to use the "real" FileReader (not the one overriden by cordova-plugin-file).
      if (file instanceof Blob) {
        const realFileReader = (fileReader as any)._realReader;
        if (realFileReader) {
          fileReader = realFileReader;
        }
      }

      fileReader.onloadend = function() {
        resolve(new Blob([this.result], { type: file.type }));
      };

      fileReader.onerror = function(err) {
        console.error('Inside fileReader.onerror', err);
        reject(err);
      };

      fileReader.readAsArrayBuffer(file);
    });
  }

The main thing is you have to check if the file is a Blob or not. If the file is a Blob, then use the real FileReader. Otherwise, don't.

Your code save my life

@glv1at118
Copy link
Contributor

So, I've found a fix (I think) is to re-order the index.html of your app so that the polyfills.js (which includes zone.js and a few other things) is included before cordova.js. This seems to make it work reliably for me. Presumably this gives some clue as to what's going on, something to do with how zone.js patches things?

Hi this solution is for writing pure html without any framework. I am using Angular, no where to touch the sequence of polyfill.js. In index.html it's not even there. Is there any solutions for angular?

@Balram143
Copy link

Balram143 commented Apr 27, 2022

@dansterrett i am using this but not work in ios...

getBlobFromFile(file: File): Promise {
r eturn new Promise((resolve, reject) => {

  let fileReader = new FileReader();

  // Is this a "real" file? In other words, is this an instance of the original `File` class (not the one overriden by cordova-plugin-file).
  // If so, then we need to use the "real" FileReader (not the one overriden by cordova-plugin-file).
  if (file instanceof Blob) {
    const realFileReader = (fileReader as any)._realReader;
    if (realFileReader) {
      fileReader = realFileReader;
    }
  }

  fileReader.onloadend = function() {
    resolve(new Blob([this.result], { type: file.type }));
  };

  fileReader.onerror = function(err) {
    console.error('Inside fileReader.onerror', err);
    reject(err);
  };

  fileReader.readAsArrayBuffer(file);
});

}
i got this error pls help ..
Screenshot 2022-04-26 at 4 56 39 PM
.

@ierome
Copy link

ierome commented May 24, 2022

Also having a problem with this. After doing all above, the file.onloadend or even onerror is not firing.

I see that in the android logs, it's attempting to read the file. But no events are firing.

15-11341/io.ionic.starter V/Capacitor/Plugin: To native (Cordova plugin): callbackId: File1270764843, service: File, action: readAsArrayBuffer, actionArgs: ["cdvfile:\/\/localhost\/files\/showMarketing",0,2]

@benthieu
Copy link

benthieu commented Apr 19, 2023

Could this be solved by adding

(window as any).__Zone_disable_FileReader = true;

to the zone-flags.ts file?
This would indicate to the zonejs to not wrap the class.
Maybe it could also fix this bug.

@aderenzis
Copy link

Could this be solved by adding

(window as any).__Zone_disable_FileReader = true;

to the zone-flags.ts file? This would indicate to the zonejs to not wrap the class. Maybe it could also fix this bug.

@benthieu it works like a charm. Thanks!

@mrov
Copy link

mrov commented Jul 17, 2023

(window as any).__Zone_disable_FileReader = true;

You saved my life hahahaha thanks <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests