I have an Ionic application, in it I have to send a converted image to base64 for an api, and api to the database. I was getting error by the size of the payload, I found on the internet about using the JSON.stringfy, apparently it worked, however, I can not get into the API and store in the database. Here are the codes used:
To perform the conversion:
getPhoto() {
let options = {
maximumImagesCount: 1
};
this.imagePicker.getPictures(options).then((results) => {
for (var i = 0; i < results.length; i++) {
this.imgPreview = results[i];
this.base64.encodeFile(results[i]).then((base64File: string) => {
this.regData.avatar = base64File;
this.novoCredenciadoModel.caminho = this.regData.avatar;
}, (err) => {
console.log(err);
});
}
}, (err) => { });
}
To send to the provider:
register() {
this.novoCredenciadoService.enviar(this.novoCredenciadoModel).subscribe((result) => {
// this.loading.dismiss();
let alert = this.alertCtrl.create({
title: 'Registration Successful',
subTitle: 'Great! Your registration is success',
buttons: ['OK']
});
alert.present();
}, (err) => {
console.log(err);
// this.loading.dismiss();
let alert = this.alertCtrl.create({
title: 'Registration Failed',
subTitle: 'Oh no! Your registration is failed',
buttons: ['OK']
});
alert.present();
});
}
And no provider:
return this.http.post(Constants.CAMINHO_TESTE+api,JSON.stringify(corpoRequisicao))
.pipe(map((resp: Response) =>{
console.log(JSON.stringify(corpoRequisicao));
console.log("post/response");
console.log (resp);
}));
In the API, the following, to receive the data:
var received = JSON.stringify(req);
var nome = received.nome;
var email = received.email;
var telefone = received.telefone;
var endereco = received.endereco;
var cnpj = received.cnpj;
var categoria = received.categoria;
var caminho = received.caminho; //caminho seria a img em base64
But I get the following error:
SyntaxError: Unexpected token P in JSON at position 0
Any way to accomplish such a function?
Edit:
handler: () => {
this.http.post("minharota/api/novo", JSON.stringify(postData), requestOptions)
.subscribe(data => {
Postdata is the data that comes from the form, which I think is correct because when I logged in it was normal.