itinance / itinance/react-native-fs
Unable to download file in Android 11 (ENOENT: no such file or directory, open '/storage/emulated/0/fileName.pdf')
- Dominant language
- C++
- Stars
- 5k
- Forks
- 1k
- PR merge metrics
- No merged PRs in 30d
Description
I am trying to download the File in Android 11 but unable to download the file. Every time it's giving ENOENT: no such file or directory, open '/storage/emulated/0/fileName.pdf' error.
FileHelper.js
```
const { PermissionsAndroid, Platform } = require("react-native");
import RNFS from "react-native-fs";
const requestStoragePermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: "Storage Permission",
message: "requires Storage Permission",
buttonNegative: "Cancel",
buttonPositive: "OK",
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
return true;
} else {
return false;
}
} catch (err) {
console.warn(err);
return false;
}
};
export const FILE_TYPE = {
UPLOADING_START: 'uploadingStart',
UPLOADING: 'uploading',
DOWNLOADING_START: 'downloading_start',
DOWNLOADING: 'downloading',
FINISHED: 'finished',
};
export const downloadPDFFile = async (
callback,
serverFilePath = "https://www.mathworksheets4kids.com/add-sub/3by2-1.pdf",
serverFileName = "3by2-1.pdf",
) => {
try {
if (Platform.OS == 'android') {
const isPermissionGranted = await requestStoragePermission();
if (!isPermissionGranted) {
alert('You have cancelled the permission');
return;
}
}
let path;
let serveFileNameReplace = serverFileName.replace(/ /g, "%20");
let serverURL = serverFilePath.replace(/ /g, "%20");
if (Platform.OS == 'ios') {
path = `${RNFS.DocumentDirectoryPath}/${serveFileNameReplace}`;
} else {
path = `${RNFS.ExternalStorageDirectoryPath}/${serveFileNameReplace}`;
}
console.log("===>",path);
RNFS.downloadFile({
fromUrl: serverURL,
toFile: path,
background: false,
cacheable: false,
connectionTimeout: 60 * 1000,
readTimeout: 120 * 1000,
begin: (res) => {
console.log('begin :- ', res);
callback({
status: FILE_TYPE.DOWNLOADING_START,
percentage: 0,
});
},
progress: (res) => {
console.log('res :- ', res);
let percentage
if(res.contentLength == -1){
percentage = 0;
}else {
percentage = (res.bytesWritten * 100) / res.contentLength;
percentage = Math.round(percentage);
}
if (percentage >= 100) {
callback({
status: FILE_TYPE.FINISHED,
percentage: percentage,
});
} else {
callback({
status: FILE_TYPE.DOWNLOADING,
percentage: percentage,
path: path,
});
}
},
}).promise.then((res) => {
console.log('res :- ', res);
callback({
status: FILE_TYPE.FINISHED,
percentage: 100,
path: path,
});
}).catch((err) => {
console.log(err);
alert(err);
});
} catch (err) {
if (err.description === 'cancelled') {
// cancelled by user
}
console.log(err);
}
};```
App.js
```
import React from 'react';
import {
SafeAreaView,
View,
Text,
StatusBar,
TouchableOpacity,
} from 'react-native';
import {downloadPDFFile} from './src/FileHelper';
const App = () => {
return (
<>
{
downloadPDFFile((res) => {
console.log(res);
});
}}>
Download File
);
};
export default App;
```
AndroidManifest:-
```
```
react-native-fs I am using to download the File. But it's not working.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.