Я использую rn-fetch-blob для загрузки файла pdf, хотя файл загружен в диспетчере загрузки на android, но он выдает и возвращает ошибку: «Диспетчер загрузки не может определить путь к загруженному файлу»
export const checkPermissionToDownload = async url => {
//Function to check the platform
//If iOS the start downloading
//If Android then ask for runtime permission
if (typeof url !== 'string') {
return Alert.alert('Liên kết không hợp lệ');
}
if (Platform.OS === 'ios') {
downloadPDF(url);
} else {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: 'Storage Permission Required',
message: 'This app needs access to your storage to download Photos',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
//Once user grant the permission start downloading
console.log('Storage Permission Granted.');
downloadPDF(url);
} else {
//If permission denied then show alert 'Storage Permission Not Granted'
alert('Storage Permission Not Granted');
}
} catch (err) {
console.log('checkPermissionToDownload -> err', err);
//To handle permission related issue
console.warn(err);
}
}
};
export const downloadPDF = fileUrl => {
try {
const {
dirs: { DownloadDir, DocumentDir },
} = RNFetchBlob.fs;
const isIOS = Platform.OS === 'ios';
const aPath = Platform.select({ ios: DocumentDir, android: DownloadDir });
const date = new Date();
const file_name = Math.floor(date.getTime() + date.getSeconds() / 2);
const fPath = `${aPath}/${file_name}.pdf`;
const configOptions = Platform.select({
ios: {
fileCache: true,
path: fPath,
appendExt: 'pdf',
},
android: {
fileCache: false,
appendExt: 'pdf',
addAndroidDownloads: {
useDownloadManager: true,
title: `${file_name}.pdf`,
description: 'Downloading...',
mime: 'application/pdf',
mediaScannable: true,
notification: true,
},
},
});
if (isIOS) {
// This iOS block works fine..
RNFetchBlob.config(configOptions)
.fetch('GET', fileUrl)
.then(async resp => {
// This options is for file sharing in iOS. Works fine.
const options = {
title: 'Receipt File', // type: 'pdf',
urls: [`file://${fPath}`],
};
await Share.open(options);
await RNFS.unlink(fPath);
})
.catch(error => {
console.log('error', error);
});
return;
}
// Android Part. Not working as expected.
RNFetchBlob.config(configOptions)
.fetch('GET', fileUrl) // This line downloads file.
.then(resp => {
console.log('resp', resp);
RNFetchBlob.android.actionViewIntent(res.path(), 'application/pdf');
// This alert pops up. But, rest.path is empty string, not undefined nor null.
Alert.alert('Success', resp.path());
})
.catch(e => {
// Failure callback registration
console.log('downloadPDF -> e', e);
Alert.alert('Tải file không thành công');
});
} catch (error) {
console.log('shareContent -> error', error);
}
};
checkPermissionToDownload('https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf')
Я обнаружил, что проблема все еще существует. https://github.com/joltup/rn-fetch-blob/issues/214 У каждого есть решение для этого?