I’m currently building a React Native application that should be able to work in offline mode. The data will be fetched from the API whenever user has internet connection and call "refresh" action.
So I would like to know the best approach to download the pdf`s files to the mobile storage, where user will be able to view them in aplication preview.
My apprehension is mostly about the pdf files, because application will download more than 100+ pdf files on update, once a month.
I’m currently having data in async storage, with the name of the pdf files, and also ready server with all the pdf files that I need to download on user`s mobile.
2
Answers
To download pdf you can use react-native-blob-util,
there is also an alternative option rn-fetch-blob.
For upload files you can use
react-native-fs
You can get an example from the package repository.
To download multiple PDF files in a React Native application, you can make use of the
react-native-fs
library, which provides a set of file system APIs for React Native. Here’s a step-by-step guide on how to achieve this:Step 1: Install the required dependencies.
Step 2: Link the library to your project.
Step 3: Request the necessary permissions for file storage.
On Android, you need to add the following permissions to your
AndroidManifest.xml
file:On iOS, you don’t need to request explicit permissions.
Step 4: Import the necessary components.
Step 5: Create a function to download the PDF files.
Step 6: Implement the
downloadFiles
function to download the PDF files.Step 7: Trigger the download process.
You can call the
downloadPDFs
function when a button is pressed or as per your application’s logic.Make sure to replace the
fileUrls
array with the actual URLs of the PDF files you want to download. Additionally, you can customize the file names and download directory to fit your requirements.Remember to handle error cases, such as when the file URL is invalid or the download fails.