Send base64 string picture using Ionic


Currently I’m using cordova-camera-preview to take a picture from the phone in order to send a request to the server. I’m sending a base64 picture to the server in JSON format, but the server cannot read the picture.

I tried the server loading pictures in base64 pictures and reads them correctly, but when trying the application, it does not work.

The code related is:

getInfo(picture): Promise<CarInfo> {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    let jsonInfo = { 'img': picture };

    return'', jsonInfo, options).toPromise()

Also the picture demonstrating that the base64 string works

So what is the problem?
Did you look at what is sent to the server to see if it is valid?
If your backend is broken - or expects a different format, there is not much we can do…

I think there is a problem with the encoding between Java and Python

if the cordova plugin parses the picture into base64 using java, first converts into utf-8 and then to base64

I will try to decode by basee64 and then decode by uft-8 to see if works


im sorry… is this the correct way to get base 64 using ionic 3?
printMyImage() {
var image = new Image();
image.onload = function() {
var canvas = document.createElement(‘canvas’);
canvas.height = 100;
canvas.width = 100;
var context = canvas.getContext(‘2d’);
context.drawImage(image, 0, 0);
var imageData = canvas.toDataURL(‘image/jpeg’).replace(/^data:image/(png|jpg|jpeg);base64,/, “”); //remove mimetype
imageData, //base64
function() {
function(error) {
image.src = ‘img/some_image.jpg’;