Skip to content

Instantly share code, notes, and snippets.

@tinwritescode
Last active April 21, 2022 23:14
Show Gist options
  • Save tinwritescode/d97107c21a46ca4dc910cc9fd8510d85 to your computer and use it in GitHub Desktop.
Save tinwritescode/d97107c21a46ca4dc910cc9fd8510d85 to your computer and use it in GitHub Desktop.
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