'Cannot be able upload image to server using formdata and axios
I'm trying to upload image to server in react native but rather than uploading image itself its uploading the local url of image which is stored in emulator or an unrecognized value if i straight away send formdata here's a code
here are console.log of following
response from picker = {"assets": [{"fileName": "rn_image_picker_lib_temp_da3dcbc9-621f-4062-a2a5-c2975114eeea.jpg", "fileSize": 172228, "height": 1280, "type": "image/jpeg", "uri": "file:///data/user/0/com.panaux/cache/rn_image_picker_lib_temp_da3dcbc9-621f-4062-a2a5-c2975114eeea.jpg", "width": 960}]}
Imagedata== ([object][object])
formData== [object Object]
formData._parts= profilePicture,[object Object]
i need to send ("profilePicture":image) it was working fine in old version of react native but then updated and getting this error
handlePickerCamera = async () => {
let formData = new FormData();
await ImagePicker.launchCamera({}, async response => {
console.log('Response = ', response);
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton);
} else {
this.setState({
ImageData:
('photo',
{
name: response.assets[0].fileName,
type: response.assets[0].type,
uri: response.assets[0].uri,
}),
});
console.log(`Image data ${this.state.ImageData}`);
formData.append('profilePicture', this.state.ImageData);
console.log(`this is required ${formData}`);
// here we can call a API to upload image on server
await axios
.patch(`${api_url1}/clients/profile`, formData, {
headers: {Authorization: `Bearer ${global.fcm_token}`},
})
.then(res => {
this.setState({
Imagess: res.data.data.profilePicture,
loaderValue: false,
});
alert('success');
})
.catch(res => {
alert(res);
});
}
});
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
