Wednesday, July 3, 2024
HomeiOS Developmentreact-native-document-picker is just not working

react-native-document-picker is just not working


I implement file uploads in react native however there’s one subject in console:
enter image description here

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

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

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

    const formData = new FormData();
    formData.append('recordsdata', {
      uri: file.uri,
      identify: file.identify,
      sort: file.sort,
    });

    strive {
      const response = await axios.publish(
        'http://localhost:1337/api/add',
        formData,
        {
          headers: {
            'Content material-Kind': 'multipart/form-data',
          },
        },
      );

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

  return (
    <View type={kinds.container}>
      <Textual content>YOUR DOCS</Textual content>
      <Button title="Choose a doc" onPress={pickDocument} />
      {file && <Textual content>{file.identify}</Textual content>}
      <Button title="Add Doc" onPress={uploadFile} />
    </View>
  );
};

the code appears to be proper, imports as nicely. I restarted an app. It didn’t assist.
Additionally I linked the dependency, after put in. I can be glad if you happen to can assist me !



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments