Hi, I made a file download function, but I don’t know where it is stored and whether it is downloaded properly.
I missing something?
const downloadFile = async () => {
if (Platform.OS === 'android') {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: 'Storage Permission',
message: 'App needs access to your storage to download the file.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
console.log(granted);
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
console.log('Permission denied');
return;
}else{
console.log('Permission ok');
// 외부 저장소의 Downloads 폴더 경로
const downloadsDir = FileSystem.documentDirectory;
console.log(downloadsDir);
// 파일 이름
const filename = 'myfile.txt';
// 파일 경로
const fileUri = `${downloadsDir}${filename}`;
// 파일 생성 및 내용 쓰기
await FileSystem.writeAsStringAsync(fileUri, 'This is the contents of the file.');
// 다운로드 URL
const url = 'https://example.com/myfile.txt';
// 다운로드 옵션
const options = {};
// 다운로드 객체 생성
const downloadObject = FileSystem.createDownloadResumable(url, fileUri, options);
// 다운로드 시작
try {
const { uri } = await downloadObject.downloadAsync();
console.log('File downloaded to:', uri);
} catch (error) {
console.log('Error downloading file:', error);
}
}
}
}