Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a file with React Native?

I am building an app with React Native, for Android and iOS. I am trying to let the user download a PDF file when clicking on a button.

  • react-native-file-download does not support Android
  • react-native-fs does nothing when I trigger downloadFile (nothing shows up on the notification bar), and I am not able to find the file after that. I added android.permission.WRITE_EXTERNAL_STORAGE to the Android Manifest file. I double-checked that the file I am trying to download exists (when it does not, the library throws an error)

I do not find other solutions for this problem. I have found libraries for viewing a PDF, but I would like to let the user download the PDF.

like image 966
Arnaud Avatar asked Jun 14 '17 13:06

Arnaud


1 Answers

Just implemented the download feature an hour ago :p

Follow these steps:

a) npm install rn-fetch-blob

b) follow the installation instructions.

b2) if you want to manually install the package without using rnpm, go to their wiki.

c) Finally, that's how I made it possible to download files within my app:

const { config, fs } = RNFetchBlob let PictureDir = fs.dirs.PictureDir // this is the pictures directory. You can check the available directories in the wiki. let options = {   fileCache: true,   addAndroidDownloads : {     useDownloadManager : true, // setting it to true will use the device's native download manager and will be shown in the notification bar.     notification : false,     path:  PictureDir + "/me_"+Math.floor(date.getTime() + date.getSeconds() / 2), // this is the path where your downloaded file will live in     description : 'Downloading image.'   } } config(options).fetch('GET', "http://www.example.com/example.pdf").then((res) => {   // do some magic here }) 
like image 74
Ray Avatar answered Sep 26 '22 07:09

Ray