Arista Anggisavitri
Arista Anggisavitri

Reputation: 31

React-native-fetch-blob downloading file

I just want my app download a file from the server by using react-native-fetch-blob. The problem is, where do the file stored? I just console.log the callback from react-native-fetch-blob and got this object

React-native-fetch-blob object callback

this is my code

alert("downloading");
RNFetchBlob
    .config({
        useDownloadManager : true, 
        fileCache : true
    })    
    .fetch('GET', 'http://fontawesome.io/assets/font-awesome-4.7.0.zip', {})
    .then((res) => {

        console.log(res);
        alert("Download");
        alert('The file saved to ', res.path());
    })

Any solution?

Upvotes: 3

Views: 29876

Answers (3)

Ayush Raichand
Ayush Raichand

Reputation: 81

function downloadFile(url,fileName) {
  const { config, fs } = RNFetchBlob;
  const downloads = fs.dirs.DownloadDir;
  return config({
    // add this option that makes response data to be stored as a file,
    // this is much more performant.
    fileCache : true,
    addAndroidDownloads : {
      useDownloadManager : true,
      notification : false,
      path:  downloads + '/' + fileName + '.pdf',
    }
  })
  .fetch('GET', url);
}

use this answer it will work for sure

Upvotes: 3

DMoon Huang
DMoon Huang

Reputation: 236

To download a file directly with rn-fetch-blob, you need to set fileCache as true.

btw, react-native-fetch-blob is not maintained anymore, use rn-fetch-blob instead

document of download file directly

RNFetchBlob
  .config({
    // add this option that makes response data to be stored as a file,
    // this is much more performant.
    fileCache : true,
  })
  .fetch('GET', 'http://www.example.com/file/example.zip', {
    //some headers ..
  })
  .then((res) => {
    // the temp file path
    console.log('The file saved to ', res.path())
  })

Upvotes: 5

Dpkstr
Dpkstr

Reputation: 1659

I am using it and it works perfectly. You just need to get the path like this.

var filePath = res.path();

this is where your file is stored.

Upvotes: 0

Related Questions