Arnaud
Arnaud

Reputation: 5112

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:

What is the right way to achieve what I want?

Upvotes: 5

Views: 3287

Answers (1)

Horacio Dillon
Horacio Dillon

Reputation: 71

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);
    })
}

Upvotes: 2

Related Questions