Expo
FileSystem
You can use the FileSystem
module to download content and save it under a directory on your device.
Installation
$ expo install expo-file-system
On a bare workflow, please follow the instructions here.
Usage
- Importing the module
import * as FileSystem from 'expo-file-system';
- Create a download resumable.
Using async/await
await FileSystem.downloadAsync(URI, FILE_URI, OPTIONS);
Using promise
FileSystem.downloadAsync(
'http://techslides.com/demos/sample-videos/small.mp4',
FileSystem.documentDirectory + 'small.mp4'
)
.then(({ uri }) => {
console.log('Finished downloading to ', uri);
})
.catch(error => {
console.error(error);
});
NOTE
Keep in mind that in order to save/store the content downloaded, you need to pass a valid directory path/uri. i.e FileSystem.documentDirectory + 'small.mp4'
. To guarantee that the path to the directory exists, you can use the FileSystem module itself to create a directory. Like this:
await FileSystem.makeDirectoryAsync(path, { intermediates: true });
. The options object takes the intermediates property that ensures that if you are trying to create a multilevel directory, it will also create the parent folder (if does not exit) for you.