how to upload image or pdf file with form-data in react-native?
You need to first use a document picker - use
react-native-document-picker
https://github.com/Elyx0/react-native-document-picker
Then create a form data object in callback of document picker
data = new FormData();
data.append('resource', {
name: res.fileName,
uri: res.uri, type: res.type
});
var req = {
method: "POST",
headers: {
'Auth-Identifier': access_token,
},
body:file
}
use above request in your fetch call. It will work for you.
jehee choi
Updated on August 21, 2022Comments
-
jehee choi almost 2 years
i found some libraries but i am not sure which one is suitable and stable for me.
i am implementing like email attachment with form-data.
Choose uploading files from gallery or file Manage on Android device and upload to server.
could you recommend nice library? thank you !
what i have found
https://github.com/kaancelen/react-native-nononsense-file-picker
https://github.com/Elyx0/react-native-document-picker
https://github.com/farmisen/react-native-file-uploader
https://github.com/huangzuizui/react-native-file-opener
and so on..
-
jehee choi about 6 yearsthanks a lot, i will try and let you know thank you!
-
Ion Andrei Bara over 3 yearsThe uri returned from React native on Android is a "content://" - uri from content provider and FormData does not know how to read that. Any workarounds for this case?