Created
December 28, 2020 11:27
-
-
Save AllanGraves/193a11bfe88d5b53eafa11b7213cf55d to your computer and use it in GitHub Desktop.
Download a File in React Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as FileSystem from 'expo-file-system'; | |
import {Image} from 'react-native'; | |
const foo = require('./assets/images/photo1.jpg'); | |
const fooURI = Image.resolveAssetSource(foo).uri; | |
<Button | |
onPress={() => { | |
let docDir = FileSystem.documentDirectory; | |
let localFile = 'file://' + docDir + 'photo1.jpg'; | |
console.log('Using :' + docDir); | |
FileSystem.downloadAsync(fooURI, localFile) | |
.then(({uri}) => { | |
console.log('Finished downloading to ' + uri); | |
FileSystem.readDirectoryAsync('file://' + docDir) | |
.then((dir) => { | |
dir.forEach((val) => { | |
console.log('File: ' + val); | |
}); | |
}) | |
.catch(() => { | |
console.log('Error reading directory: ' + docDir); | |
}); | |
}) | |
.catch((error) => { | |
console.error(error); | |
}); | |
}} | |
title="click me" | |
/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment