0
I have an Ionic application, in it I have to send an image converted to Base64 for an api, and the api for the database. I was getting error by the size of the payload, I found on the internet about using JSON.stringfy, apparently worked, however, I can not receive in the API and store in the database. Follows 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 past:
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 in the past:
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
Some way to perform 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 are correct because when loguei was normal.
Performing this imageEncoding, I will then be able to show the image in the app normally or I would have to perform some specific coding ?
– Prostetnic Vogon Jeltz
You could, and you don’t need a code name. This "req.body.image.replace(/g, "+");" is required to handle the content that was transformed by "application/x-www-form-urlencoded" during the request.
– Fernando Matos
I was testing here, and I came across the following scenario: If I don’t use the bodyparser there to increase the limit, it gives payload to large, OK. But if I use it, it says that the image comes Undefined, and the error in parse. I did exactly as you recommended, is that something left behind ?
– Prostetnic Vogon Jeltz
Please send the Request code that is being fired to your api?
– Fernando Matos
edited the question, put at the end
– Prostetnic Vogon Jeltz
– Fernando Matos
– Fernando Matos
example of an HTTP message for this purpose: PUT /api/users/alterarImagem HTTP/1.1 Host: localhost:3000 Authorization: Bearer eyJ0eXAiOJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIOiIwNjYuMjcwLI5Ni00MiJ9.9lX3rMFi42EI1zmRcLzalw_S874N7dAK8_MS66BOMQ Content-Type: application/x-www-form-urlencoded Cache-Control: no-cache Postman-Token: afe76ca1-d3c1-6eb6-bd1c-4c20d1e47da9 image=/9j/4AQSkZJRgABAQEBLAE ...
– Fernando Matos
First use Postman, make sure the HTTP message exchange works and then you implement it in the technology you want
– Fernando Matos