0
I have an app in React on, I have an option that the user can change his avatar, I am using the library ImagePicker
and I have the API in Laravel.
I’m having trouble accessing the chosen image in the room I’m giving a request->file('avatar')
but returns null
I don’t know why, what I’m doing wrong ?
React Native
handleChoosePhoto = () => {
const options = {
title: "Escolher Foto",
takePhotoButtonTitle: "Tirar Foto...",
chooseFromLibraryButtonTitle: "Escolher da Galeria...",
mediaType: "photo",
storageOptions: {
skipBackup: true,
path: "images"
}
};
ImagePicker.showImagePicker(options, response => {
console.log(response);
if (response.didCancel) {
console.log("User cancelled image picker");
} else if (response.error) {
showMessage({
message: "Foto de Perfil",
description: response.error,
type: "danger",
floating: true,
duration: 2850
});
} else {
this.handleUploadPhoto(response);
}
});
};
handleUploadPhoto = async responseSource => {
try {
const id = this.props.user.id;
const data = new FormData();
const uriPart = responseSource.uri.split(".");
const fileExtension = uriPart[uriPart.length - 1];
data.append("avatar ", {
uri: responseSource.uri,
name: `avatar.${fileExtension}`,
type: `image/${fileExtension}`
});
const response = await api.post(`user/dados/${id}/avatar`, {
data
});
console.log(response.data);
showMessage({
message: "Perfil",
description: "Dados actualizados com sucesso!",
type: "success",
floating: true,
duration: 2850
});
} catch (error) {}
};
Laravel API
$user = $request->user_id;
avatar = request->file('avatar');
return response()->json(['avatar' => $avatar]);
yes api and an instance of Axios, I tried to add this and nothing I put in the api of the Laravel a $request->file('avatar') to check by the console if it caught but nothing returns null
– César Sousa