Created
September 25, 2022 06:05
-
-
Save Saad-Bashar/62781abe5699ad47864eb105958118d1 to your computer and use it in GitHub Desktop.
downloading pdf files in react native
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const downloadFile = () => { | |
| const source = "https://www.africau.edu/images/default/sample.pdf"; | |
| let dirs = ReactNativeBlobUtil.fs.dirs; | |
| ReactNativeBlobUtil.config({ | |
| fileCache: true, | |
| appendExt: 'pdf', | |
| path: `${dirs.DocumentDir}/${fileName}`, | |
| addAndroidDownloads: { | |
| useDownloadManager: true, | |
| notification: true, | |
| title: fileName, | |
| description: 'File downloaded by download manager.', | |
| mime: 'application/pdf', | |
| }, | |
| }) | |
| .fetch('GET', fileUrl) | |
| .then((res) => { | |
| // in iOS, we want to save our files by opening up the saveToFiles bottom sheet action. | |
| // whereas in android, the download manager is handling the download for us. | |
| if (Platform.OS === 'ios') { | |
| const filePath = res.path(); | |
| let options = { | |
| type: 'application/pdf', | |
| url: filePath, | |
| saveToFiles: true, | |
| }; | |
| Share.open(options) | |
| .then((resp) => console.log(resp)) | |
| .catch((err) => console.log(err)); | |
| } | |
| }) | |
| .catch((err) => console.log('BLOB ERROR -> ', err)); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Saad-Bashar source and filename missing