Search code examples
cordovaionic-frameworkionic2cordova-pluginsionic3

Base64 plugin not working


The plugin was working fine until recently I started facing problems with it, the code does not execute neither does it throw an error. I want to capture images from camera and send the base64 string to the server, which was easy since I could directly get the base64 of the captured image but then I used the native crop which returns the URI of the cropped image. So now I HAVE to get the base64 of this image, but the Base64 plugin isn't working anymore. Any workaround or help is really appreciated. Code I used:

this.base64.encodeFile(filePath).then((base64File: string) => {
    console.log(base64File);    // Won't execute
}, (err) => {
    console.log(err);  // Won't execute
});

Solution

  • I spent couple of hours on the same bug. It occurred that this plugin just stopped working (it's still beta version). I made some changes in my code and replaced Base64 plugin with File Plugin.

    1. Add File plugin to app.module.ts imports.
    2. Import and inject dependency in component/service constructor.

    And then if you have filepath:

    // split file path to directory and file name
    let fileName = filePath.split('/').pop();
    let path = filePath.substring(0, filePath.lastIndexOf("/") + 1);
    
    this.file.readAsDataURL(path, fileName)
    .then(base64File => {
        console.log("here is encoded image ", base64File)
    })
    .catch(() => {
        console.log('Error reading file');
    })