I'm using EXPO-CLI's image picker for uploading images and got them to render client sided but now trying to send them to my Laravel REST API and move them to a folder. The problem is, laravel isn't recognizing my formdata image via react-native as a file.
React Native Code:
const [image, setImage] = useState('');
const [data, setData] = useState('');
pickImage = async () => {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsEditing: true,
aspect: [1, 1]
});
console.log(result);
if (!result.cancelled) {
setImage(result.uri);
setData(result)
}
};
const addNew = () => {
const formData = new FormData();
formData.append("name", name);
formData.append("description", description);
formData.append("file", {type: 'image/jpg', uri:image, name:'uploaded.jpg'});
formData.append("price", price);
formData.append("category", categoryID);
const heads =
{
headers: {
'content-type': 'multipart/form-data'
}
}
axios
.post(
"http://mydomain/products/store",
formData,
heads
)
.then(res => {
console.log(res.data);
setProducts([...products, res.data]);
})
.catch(err => {
console.log(err);
});
};
Just what I'm checking for on laravel so far:
return response()->json($request->hasFile('file'), 200);
Which returns false for some reason.
Sorry I'm pretty new to react-native.