Last active
April 21, 2022 23:14
-
-
Save tinwritescode/d97107c21a46ca4dc910cc9fd8510d85 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { StatusBar } from "expo-status-bar"; | |
import { StyleSheet, Text, View } from "react-native"; | |
import { TouchableOpacity } from "react-native"; | |
import * as DocumentPicker from "expo-document-picker"; | |
import RNFetchBlob from 'rn-fetch-blob' | |
export default function App() { | |
const onPress = async () => { | |
let result = await DocumentPicker.getDocumentAsync({ | |
// type: "*/*" // all files | |
type: "image/*", // all images files | |
}); | |
try { | |
const { uri, mimeType: type, name } = result; | |
console.log(uri, type, name); | |
const resp = await FetchBlob.fetch( | |
"POST", | |
"https://URL/test/api-form-data", | |
{ | |
Authorization: "Bearer access-token", | |
otherHeader: "foo", | |
"Content-Type": "multipart/form-data", | |
}, | |
[ | |
{ name: 'image' , filename: name, type, data: RNFetchBlob.wrap(uri)}, | |
] | |
); | |
console.log("Success", resp); | |
} catch (error) { | |
console.log("Error", error.message); | |
} | |
}; | |
return ( | |
<View style={styles.container}> | |
<StatusBar style="auto" /> | |
<TouchableOpacity onPress={onPress}> | |
<Text style={{ backgroundColor: "red", color: "white", padding: 20 }}> | |
Choose file | |
</Text> | |
</TouchableOpacity> | |
</View> | |
); | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
backgroundColor: "#fff", | |
alignItems: "center", | |
justifyContent: "center", | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment