I was having some problem when trying to upload image from React Native to AWS Server. Here is my code:
async function uploadImageAsync(uri) {
console.log(uri);
let apiUrl = '...'
let uriParts = uri.split('.');
let fileType = uri[uri.length - 1];
let formData = new FormData();
formData.append('image', {
uri,
name: `image.jpg`,
filename: `image.jpg`,
});
let options = {
method: 'POST',
body: formData,
headers: {
Accept: 'application/json',
'Content-Type': 'multipart/form-data',
},
};
return fetch(apiUrl, options);
}
The results that printed out at the console are:
file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540gh%252Ftp2/ImagePicker/37c89140-2172-4566-98a2-e7393ea72e89.jpg
Object {
"uploadResponse": undefined,
}
Object {
"uploadResult": undefined,
}
Object {
"e": [TypeError: Network request failed],
}
What I did is I pick an image from image picker, then I will upload it to AWS server. If success, print out success message. If failed, print out those in the console above.
The strange thing is, it managed to upload on iOS but not Android. Any ideas why is it so?
Thanks!
See Question&Answers more detail:os