0
I’m having trouble sending in the same request a photo and a json by Postman.
I need to send the profile photo of the user and at the same time receive a json with the data from it.
//Instância da entidade Login
$login = new Login();
//recuperando o parâmetro objeto login do json
$fk_login_cidadao = $request->getParam('fk_login_cidadao');
//setando valores do objeto login
$login ->setLogin($fk_login_cidadao['login']);
$login ->setEmail($fk_login_cidadao['email']);
$login ->setSenha($fk_login_cidadao['senha']);
$login ->setStatus_login($fk_login_cidadao['status_login']);
$login ->setAsAdministrador($fk_login_cidadao['administrador']);
//salvando login
$entityManager->persist($login);
$entityManager->flush();
//buscando login recém salvo
$loginRepository = $entityManager->getRepository('App\Models\Entity\Login');
//pegando login
$loginCidadao = $loginRepository->find($login->getId_login());
//Salvar foto
$files = $request->getUploadedFiles();
$newimage = $files['foto'];
if ($newimage->getError() === UPLOAD_ERR_OK) {
$uploadFileName = $newimage->getClientFilename();
$type = $newimage->getClientMediaType();
$name = uniqid('img-' . date('d-m-y') . '-');
$name .= $newimage->getClientFilename();
// $imgs[] = array('url' => '/Photos/' . $name);
//local server
$newimage->moveTo("C:\Users\jvict\PhpstormProjects\api.webservice\img");#/home/citycare/Imgs/User/$name
//localdev
$photoURL = "C:\Users\jvict\PhpstormProjects\api.webservice\img";#/home/citycare/Imgs/User/$name
}
//Instância da entidade Cidadao
$cidadao = new Cidadao();
//setando valores do objeto cidadao
$cidadao ->setFk_login_cidadao($loginCidadao);
$cidadao->setNome($request->getParam('nome'));
$cidadao->setSexo($request->getParam('sexo'));
$cidadao ->setSobrenome($request->getParam('sobrenome'));
$cidadao ->setEstado($request->getParam('estado'));
$cidadao ->setCidade($request->getParam('cidade'));
$cidadao ->setDir_foto_usuario($request->getParam($photoURL));
$entityManager->persist($cidadao);
$entityManager->flush();
//retornando confirmação do evento completo
$return = $response->withJson(["result" => true],201)->withHeader('Content-type', 'application/json');
Have you tried to change the content-type to Multipart/form-date? Another solution would be to send the Base64 encoded image inside the JSON with the information and in PHP decode the Base64 and generate the file.
– Renato Diniz
with the content-type so n works, I’m getting the img of an android app, so in case p encode p Base64 would be in the app ?
– João Victor
Yes, it is possible to encode for Base64 in Java on Android.
– Renato Diniz
Thanks friend!
– João Victor
The solution worked out?
– Iago Frota