2

My FileReader object is working perfectly to upload images. I'm tracking onload as well as other events, these all happen when I upload images:

fileReader = new FileReader();

fileReader.onloadstart = (e) => {
  window.alert('fileReader onloadstart');
  window.alert(e);
};

// ... and the same for onprogress, onabort, onerror, then finally:

fileReader.onload = (e) => {
  window.alert('arrived to fileReader.onload!');
  // ...
};

I added cordova-plugin-media-capture to capture audio. I now need to use the existing fileReader.onload callback to upload it etc.

But no matter what I do, I cannot get fileReader.readAsDataURL to respond. Below is my code adapted from this answer about a similar problem, but it doesn't resolve it.

None of the fileReader events are firing, not even error. The fileReader.readAsDataURL function is available, yet when calling it all that happens is the screen briefly goes white and then I'm back at the page I was at before as if I had not done anything. None of the methods on fileReader show their alerts.

  navigator.device.capture.captureAudio((files) => {
      const file = files[0];
      newFile = new File(
        file.name,
        file.localURL,
        file.type,
        file.lastModifiedDate,
        file.size);

      window.alert(newFile); 
      // --> [Object object]

      window.alert(JSON.stringify(newFile));
      // --> An object with name, localURL etc. See image.

      window.alert(fileReader.readAsDataURL);
      // --> function readAsDataURL() { [native code] }

      fileReader.readAsDataURL(newFile); // nothing at all
  })

The stringified newFile object is:

newFile stringified

I've tried processing the file captureAudio gives in different ways:

But every time the same thing: when I arrive at readAsDataURL, nothing happens anymore. What could be wrong?

Edit: I forgot to mention some things:

  • tested on iOS device and simulator
  • cordova-plugin-file is installed
Bart S
  • 1,698
  • 1
  • 16
  • 21
  • Please try installing this one as well: cordova plugin add cordova-plugin-file – Niels Steenbeek Jul 10 '18 at 11:56
  • The project already included `cordova-plugin-file`, sorry I forgot to mention it. Thanks @NielsSteenbeek, do you have any idea what else may be wrong? – Bart S Jul 10 '18 at 12:26
  • 1
    Maybe copy-paste my example 1:1 and see whether onload is invoked. If that works, you can encapsulate in a promise. – Niels Steenbeek Jul 10 '18 at 14:07
  • 1
    Thanks @NielsSteenbeek! It turns out defining a new FileReader solves the problem. I still don't understand why I cannot access the existing one... even copying over the existing `onload` to the new one works fine. But well, it works. Thanks a lot! – Bart S Jul 10 '18 at 14:58

0 Answers0