I have my own server (https://myserverforimg/upload) where the photo is converted to url. For example, any photo is sent there and we get its address:
Response: https://myserverforimg/public/3d5d9d74-b539-4ef5-8948-9d2121532a25
This address can be used like:
<Image style={styles.logo} source={{uri: ‘https://myserverforimg/public/3d5d9d74-b539-4ef5-8948-9d2121532a25‘, }} />
In my program, I am using the ImagePicker from expo-image-picker. And I’m trying to send a photo to the server, but it doesn’t work … Please help me solve the problem
This is how I’m trying to implement it:
const [image, setImage] = useState(null);
const pickImageProfile = async () => {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsEditing: true,
aspect: [3, 4],
quality: 1,
base64: true,
});
if (!result.canceled) {
setImage(result.assets[0].uri);
fdata.client_image = result.assets[0].uri
const file = result.assets[0].base64;
const formData = new FormData();
formData.append('file', file);
axios.post("https://lis.4dev.kz/upload", formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
})
.then((response) => {
console.log('response created')
console.log(response)
}).catch((error) => {
console.log(error);
});
}
};
2
Answers
For mimeType of file, you need to install mime using
npm i mime
A file object for
FormData
consists of three parts: uri, type and name. No need to use base64 in options as uri is used. Something like this should work: