rn-fetch-blob
rn-fetch-blob copied to clipboard
Failed to download a pdf file with post method
I have a downloadFile function using post method and authorization header. But the fetch always catch error
{"line":285897,"column":34,"sourceURL":"http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.bsw_rn&modulesOnly=false&runModule=true"}
This error doesn't contain clear message why the download is failed. Am i missing something?
Installed libraries : "react": "17.0.2" "react-native": "0.68.2" "rn-fetch-blob": "^0.12.0" "react-native-blob-util": "^0.13.18"
const downloadFile = async (url) => {
// Get today's date to add the time suffix in filename
let date = new Date();
// config: To get response by passing the downloading related options
// fs: Root directory path to download
const { config, fs } = RNFetchBlob;
let RootDir = fs.dirs.DCIMDir;
let path=RootDir+ '/file_' + Math.floor(date.getTime() + date.getSeconds() / 2) +'.pdf';
let options = {
fileCache: true,
addAndroidDownloads: {
path: path,
description: 'downloading file...',
notification: true,
// useDownloadManager works with Android only
useDownloadManager: true,
mime: 'application/pdf',
overwrite: true,
fileCache: true,
},
};
//Check if directory exist
if(await RNFS.exists(RootDir)){
config(options)
.fetch('POST', url,{
'Authorization' : `Bearer ${auth.access_token}`
})
.then(res => {
console.log('res.path',res.path());
}).catch(err => {
console.log(JSON.stringify(err))
});
console.log('RootDir exists');
}else{
RNFS.mkdir(RootDir).then(()=>{
config(options)
.fetch('POST', url,{
'Authorization' : `Bearer ${auth.access_token}`
})
.then(res => {
console.log('res.path',res.path());
}).catch(err => {
console.log(JSON.stringify(err))
});
});
console.log('RootDir doesn\'t exist');
}
};
Permission checking
// Function to check the platform
// If Platform is Android then check for permissions.
const checkPermission = async () => {
if (Platform.OS === 'ios') {
downloadFile(url);
} else {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: 'Storage Permission Required',
message:
'Application needs access to your storage to download File',
}
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
// Start downloading
downloadFile(url);
} else {
// If permission denied then show alert
Alert.alert('Error','Storage Permission Not Granted');
}
} catch (err) {
// To handle permission related exception
console.log("++++"+err);
}
}
};
AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:allowBackup="false"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
android:requestLegacyExternalStorage="true"
>
@galih56 were you able to solve this issue ?
@galih56 @hrahul2605 Are you able to solve this issue ?
I'm having same issue. Does anybody solve this issue. I can't able to download pdf file using post method. Can anyone please help me out of this.