0.0.10 • Published 9 years ago
react-native-file-download v0.0.10
React Native File Download

Native file download utility for react-native
Note that does not support Android.
Breaking change -- Now supports React Native v25+. Not compatible with prior versions. You can continue using older versions of React Native with react-native-file-download@0.0.9.
Installation
npm install react-native-file-download --saveGetting started - iOS
- In XCode, in the project navigator right click
Libraries➜Add Files to [your project's name] - Go to
node_modules➜react-native-file-downloadand addRNFileDownload.xcodeproj - Add
libRNFileDownload.a(from 'Products' under RNFileDownload.xcodeproj) to your project'sBuild Phases➜Link Binary With Librariesphase - Look for Header Search Paths and make sure it contains both
$(SRCROOT)/../react-native/Reactand$(SRCROOT)/../../React- mark both as recursive - Run your project (
Cmd+R)
Usage
require it in your file
const FileDownload = require('react-native-file-download')you may also want to use something like react-native-fs to access the file system (check its repo for more information)
const RNFS = require('react-native-fs')API
download(source: string, target: string): Promise
download file from source to target
Example
const URL = '/path/to/remote/file'
const DEST = RNFS.DocumentDirectoryPath
const fileName = 'zip.zip'
const headers = {
'Accept-Language': 'en-US'
}
FileDownload.download(URL, DEST, fileName, headers)
.then((response) => {
console.log(`downloaded! file saved to: ${response}`)
})
.catch((error) => {
console.log(error)
})addListener(source: string, callback: function): EmitterSubscription
event listener for progress of download
Example
const URL = '/path/to/remote/file'
const DEST = RNFS.DocumentDirectoryPath
const fileName = 'zip.zip'
const headers = {
'Accept-Language': 'en-US'
}
FileDownload.addListener(URL, (info) => {
console.log(`complete ${(info.totalBytesWritten / info.totalBytesExpectedToWrite * 100)}%`);
});
FileDownload.download(URL, DEST, fileName, headers)
.then((response) => {
console.log(`downloaded! file saved to: ${response}`)
})
.catch((error) => {
console.log(error)
})