Friday, May 17, 2024
172
rated 0 times [  174] [ 2]  / answers: 1 / hits: 5898  / 3 Years ago, tue, april 6, 2021, 12:00:00

I'm new in react native. I'm learning it but I'm facing an issue working on expo document picker. I use the document picker, but it does not display anything showing or sometimes gives a promise rejection error.
I'm stuck at it for a long time today.


upload.js


import React, { useState } from "react";
import {
StyleSheet,
Text,
View,
TextInput,
Button,
TouchableOpacity,
} from "react-native";
import * as DocumentPicker from "expo";

const UploadFile = () => {
pickDocument = async () => {
let result = await DocumentPicker.getDocumentAsync({});
console.log(result.uri);
console.log(result);
};

return (
<View style={styles.background}>
<Text style={styles.file}>Upload CSV File</Text>
<View style={styles.button}>
<TouchableOpacity>
<Button
title="upload your file"
color="black"
onPress={pickDocument}
/>
</TouchableOpacity>
</View>
</View>
);
};

const styles = StyleSheet.create({
background: {
backgroundColor:
"radial-gradient(ellipse at left bottom, rgb(163, 237, 255) 0%, rgba(57, 232, 255, 0.9) 59%, rgba(48, 223, 214, 0.9) 100% )",
},
file: {
color: "black",
marginHorizontal: 145,
},
button: {
marginHorizontal: 60,
},
});

export default UploadFile;

I want to upload a file.


More From » react-native

 Answers
4

Firstly install expo-document-picker


Secondly, Your Import Statement is wrong


On the third line you wrote
import * as DocumentPicker from 'expo'; but you have to write import * as DocumentPicker from 'expo-document-picker';


I have corrected it. Check once.


import React, { useState } from "react";
import {
StyleSheet,
Text,
View,
TextInput,
Button,
TouchableOpacity,
} from "react-native";
import * as DocumentPicker from "expo-document-picker";

const UploadFile = () => {
const pickDocument = async () => {
let result = await DocumentPicker.getDocumentAsync({});
console.log(result.uri);
console.log(result);
};

return (
<View style={styles.background}>
<Text style={styles.file}>Upload CSV File</Text>
<View style={styles.button}>
<TouchableOpacity>
<Button
title="upload your file"
color="black"
onPress={pickDocument}
/>
</TouchableOpacity>
</View>
</View>
);
};

const styles = StyleSheet.create({
background: {
backgroundColor:
"radial-gradient(ellipse at left bottom, rgb(163, 237, 255) 0%, rgba(57, 232, 255, 0.9) 59%, rgba(48, 223, 214, 0.9) 100% )",
},
file: {
color: "black",
marginHorizontal: 145,
},
button: {
marginHorizontal: 60,
},
});

export default UploadFile;

[#1522] Wednesday, March 31, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hailie

Total Points: 25
Total Questions: 112
Total Answers: 111

Location: Belize
Member since Tue, Dec 8, 2020
4 Years ago
hailie questions
;