I implement file uploads in react native but there is one issue in console:
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 !