Simple Download File React Native

Hello , react native i think very easy to use and learn. If you need to download something(file, image, pdf, etc) you can use react-native-fetch-blob. May be i want to share part of my project.

You can use this library :

https://github.com/wkh237/react-native-fetch-blob

Features Library

  • Transfer data directly from/to storage without BASE64 bridging
  • File API supports regular files, Asset files, and CameraRoll files
  • Native-to-native file manipulation API, reduce JS bridging performance loss
  • File stream support for dealing with large file
  • Blob, File, XMLHttpRequest polyfills that make browser-based library available in RN (experimental)
  • JSON stream supported base on Oboe.js

It’ sample code for download file :

download(){
var date = new Date();
var url = "http://www.clker.com/cliparts/B/B/1/E/y/r/marker-pin-google-md.png";
var ext = this.extention(url);
ext = "."+ext[0];
const { config, fs } = RNFetchBlob
let PictureDir = fs.dirs.PictureDir
let options = {
fileCache: true,
addAndroidDownloads : {
useDownloadManager : true,
notification : true,
path: PictureDir + "/image_"+Math.floor(date.getTime() + date.getSeconds() / 2)+ext,
description : 'Image'
}
}
config(options).fetch('GET', url).then((res) => {
Alert.alert("Success Downloaded");
});
}
extention(filename){
return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined;
}

Thanks for view my article, i hope it’s can help you.

Show your support

Clapping shows how much you appreciated Derry Berni Cahyady’s story.