Professional Documents
Culture Documents
How To Upload Multiple Files React Native - by Ariel Salem - Medium
How To Upload Multiple Files React Native - by Ariel Salem - Medium
How To Upload Multiple Files React Native - by Ariel Salem - Medium
Ariel Salem
92 Followers About Follow
After spending the better part of week wrestling with React-Native packages and native
JavaScript implementations to allow users to upload photos from their phone to the
database in bundles, I finally found a simple native solution that’s quick and easy to use.
This solution was derived via a trial and error process that I hope no one else will have to
go through.
Setup A Form:
Our first step in uploading our files as a bundle would be to create a FormData object.
this.state.selectedImages.forEach((item, i) => {
data.append("doc[]", {
uri: item.uri,
https://medium.com/@ariel.salem1989/how-to-upload-multiple-files-react-native-e9577a5de106 1/3
10/31/21, 6:02 PM How to Upload Multiple Files React Native | by Ariel Salem | Medium
type: "image/jpeg",
});
});
Once we’ve finished appending the relevant data onto our FormData, we can go ahead
and submit our data using JavaScript’s native fetch method.
Submit Data:
Fortunately for us, JavaScript’s fetch method allows us to send multipart/form-data as
easily as you would any other data.
fetch(`${config.apiBase}/load/${this.props.id}/uploadconfirmation`, {
method: "post",
headers: {
Accept: "application/x-www-form-urlencoded",
},
body: data,
.then(res => {
Alert.alert(
"Success",
{ cancelable: false }
);
})
.catch(err => {
});
There you have it! If that helped you avoid a headache, feel free to applaud this post!
https://medium.com/@ariel.salem1989/how-to-upload-multiple-files-react-native-e9577a5de106 2/3
10/31/21, 6:02 PM How to Upload Multiple Files React Native | by Ariel Salem | Medium
https://medium.com/@ariel.salem1989/how-to-upload-multiple-files-react-native-e9577a5de106 3/3