I’ve been trying to solve this issue for days but I couldn’t find any solution, I don’t know if this is a problem with the expo version, I’m using the 35.0.0, in the app, I want the user to insert a description in a TextInput an also take a picture with the camera, I’m using expo-image-picker, everything works perfectly if I only send text to my database but when I implemented formData to send the file I get this error on my android device and emulator, I’m using localhost, so my devices are in the same network.
Here is my code:
constructor(props){
super(props)
this.state = {
InputDescripcion:'',
image:null
}
}
This is the method I’m using to post the content
InsertUser = ()=>{
//constant varaibles that equal propertes in state
const {InputDescripcion} = this.state;
const {image} = this.state;
const formData = new FormData();
formData.append('Descripcion', InputDescripcion);
formData.append('photo', {
uri: Platform.OS === "android" ? image.uri : image.uri.replace("file://", ""),
type: 'image/jpeg',
name: 'photo.jpeg'});
//API that use fetch to input data to database via backend php script
fetch('http://192.168.1.59/911Occidente/TestAlert.php',{
method: 'POST',
Accept: 'application/json',
'Content-Type': 'multipart/form-data',
body: formData
})
.then((response) => response.text())
.then((responseJson) =>{
//alert(responseJson);
if(responseJson == "ok"){
alert("Success");
}else{
alert(responseJson);
}
})
.catch((error)=>{
console.error(error);
});
Keyboard.dismiss();
}
Here is the render method:
render(){
let { image } = this.state;
return(
<Button
title="Add Image"
onPress={this.takePicture}
/>
<Button
style = {{marginTop:20}}
title="Reset"
onPress={this.resetImage}
/>
</View>
<View style={{alignSelf:"center"}}>
{image &&
<Image source={{uri:image }} style={{ width: 300, height: 300 }} />}
</View>
<View style={{marginTop:100, alignSelf:'center'}}>
<TouchableOpacity style={styles.button} onPress={this.InsertUser}>
<Text style={styles.buttonText} > Send</Text>
</TouchableOpacity>
</View>
):
}
I’ve been looking for many solutions but any of them worked for me, I get this error when I want to use formData, could this be related to my expo version?