Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download files on iOS with React Native

I am building an app that lets the user download a PDF file when he clicks on a button.

I use the library react-native-fetch-blob for this purpose.

It works well on Android.

On iOS, the console log tells me the download has worked well, but:

  • I don't see anything happening on screen (on Android, there is a notification that tells me the download is complete)
  • I cannot find the file anywhere on the iPad. There is no "My files" folder or equivalent.

What is the right way to achieve what I want?

like image 409
Arnaud Avatar asked Nov 08 '22 20:11

Arnaud


1 Answers

The solution thats worked for me in iOS was use RNFetchBlob.ios.openDocument function inside the then of fetch. This function open the file on the device file explorer where you have the option to download. Here is the code example:

downloadPDFiOS (uri) {
let uriSplitted = uri.split("/");
const fileName = uriSplitted.pop();
const dirs = RNFetchBlob.fs.dirs;
RNFetchBlob
    .config({                
        path: dirs.DocumentDir + '/' + fileName,
        fileCache: true,
    })
    .fetch('GET', uri, {
        //some headers ..
    })
    .then((res) => {
        RNFetchBlob.ios.openDocument(res.data);
    })
    .catch((e) => {
        console.log('Error en el fetch: ', e);
    })
}
like image 82
Horacio Dillon Avatar answered Nov 15 '22 04:11

Horacio Dillon