I have the VueJS component installed without problem in my project with Symfony 4 but at the moment I want to upload an image. I follow this reference from Laravel: How to upload image from VueJS to Laravel with Axios?
I get to the controller but that's where the value in base 64 does not reach just the console message.
Code:
//CargaFoto.vue
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<input type="file" name="image" @change="getImage" accept="image/*">
<button @click="updateAvatar">Subir Imagen</button>
</div>
</div>
</template>
<script>
import axios from 'axios'
export default {
name: "CargaFoto",
data() {
return {
msg: "Cargar Imagen de Perfil",
imagen: null
};
},
methods: {
getImage(event){
//Asignamos la imagen a nuestra data
this.imagen = event.target.files[0];
},
updateAvatar(){
//Creamos el formData
var data = new FormData();
data.append('avatar', this.imagen);
data.append('_method', 'POST');
//Enviamos la petición
axios.post('/usuario/jsonimagen',data)
.then(response => {
console.log(res)
})
}
</script>
And this the controller code:
/**
* @return JsonResponse
* @Route("/jsonimagen", name="jsonimagen", methods="POST")
*/
public function jsonimagen(Request $request):JsonResponse
{
$data= $request->get("data");
return $this->json($data);
}
The answer is null The doubt that I have is how I upload the image to the local server.
