0

I implement file uploads in react native but there is one issue in console: enter image description here

const [file, setFile] = useState(null);

  const pickDocument = async () => {
    try {
      const res = await DocumentPicker.pick({
        type: [DocumentPicker.types.allFiles],
      });
      setFile(res);
    } catch (err) {
      if (DocumentPicker.isCancel(err)) {
        console.log('User cancelled the picker');
      } else {
        console.log('DocumentPicker Error: ', err);
      }
    }
  };

  const uploadFile = async () => {
    if (!file) return;

    const formData = new FormData();
    formData.append('files', {
      uri: file.uri,
      name: file.name,
      type: file.type,
    });

    try {
      const response = await axios.post(
        'http://localhost:1337/api/upload',
        formData,
        {
          headers: {
            'Content-Type': 'multipart/form-data',
          },
        },
      );

      console.log(response.data);
    } catch (error) {
      console.error(error);
    }
  };

  return (
    <View style={styles.container}>
      <Text>YOUR DOCS</Text>
      <Button title="Pick a document" onPress={pickDocument} />
      {file && <Text>{file.name}</Text>}
      <Button title="Upload Document" onPress={uploadFile} />
    </View>
  );
};

the code seems to be right, imports as well. I restarted an app. It did not help. Also I linked the dependency, after installed. I will be glad if you can help me !

1
  • try installing pods. run below command in console ``` cd ios && pod install && cd .. ``` Commented Jul 1 at 13:35

1 Answer 1

0

It seem that your package is not installed properly did you try this install agin

yarn add react-native-document-picker

and also follow to documentation of React Native document picker for public package

React Native document picker

Not the answer you're looking for? Browse other questions tagged or ask your own question.