Skip to content

Instantly share code, notes, and snippets.

@AllanGraves
Created December 28, 2020 11:27
Show Gist options
  • Save AllanGraves/193a11bfe88d5b53eafa11b7213cf55d to your computer and use it in GitHub Desktop.
Save AllanGraves/193a11bfe88d5b53eafa11b7213cf55d to your computer and use it in GitHub Desktop.
Download a File in React Native
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