What is expo-file-system?
The expo-file-system package provides access to the file system on the device, allowing for reading, writing, deleting, and managing files and directories. It is part of the Expo ecosystem and is designed to work seamlessly with other Expo modules.
What are expo-file-system's main functionalities?
Reading Files
This feature allows you to read the contents of a file from a given URI. The readAsStringAsync method reads the file and returns its content as a string.
import * as FileSystem from 'expo-file-system';
async function readFile(uri) {
try {
const content = await FileSystem.readAsStringAsync(uri);
console.log(content);
} catch (error) {
console.error('Error reading file:', error);
}
}
Writing Files
This feature allows you to write content to a file at a specified URI. The writeAsStringAsync method writes the provided content to the file.
import * as FileSystem from 'expo-file-system';
async function writeFile(uri, content) {
try {
await FileSystem.writeAsStringAsync(uri, content);
console.log('File written successfully');
} catch (error) {
console.error('Error writing file:', error);
}
}
Deleting Files
This feature allows you to delete a file at a specified URI. The deleteAsync method deletes the file.
import * as FileSystem from 'expo-file-system';
async function deleteFile(uri) {
try {
await FileSystem.deleteAsync(uri);
console.log('File deleted successfully');
} catch (error) {
console.error('Error deleting file:', error);
}
}
Creating Directories
This feature allows you to create a directory at a specified URI. The makeDirectoryAsync method creates the directory, and the intermediates option allows for creating intermediate directories if they do not exist.
import * as FileSystem from 'expo-file-system';
async function createDirectory(uri) {
try {
await FileSystem.makeDirectoryAsync(uri, { intermediates: true });
console.log('Directory created successfully');
} catch (error) {
console.error('Error creating directory:', error);
}
}
Downloading Files
This feature allows you to download a file from a remote URI to a local URI. The downloadAsync method handles the download process.
import * as FileSystem from 'expo-file-system';
async function downloadFile(uri, downloadUri) {
try {
const { uri: localUri } = await FileSystem.downloadAsync(downloadUri, uri);
console.log('File downloaded to:', localUri);
} catch (error) {
console.error('Error downloading file:', error);
}
}
Other packages similar to expo-file-system
react-native-fs
react-native-fs is a file system library for React Native that provides similar functionalities to expo-file-system, such as reading, writing, and managing files and directories. It is more flexible in terms of platform support but requires linking and additional setup compared to the seamless integration of expo-file-system within the Expo ecosystem.
rn-fetch-blob
rn-fetch-blob is a library that provides file system access and network capabilities for React Native. It allows for file manipulation, downloading, and uploading, similar to expo-file-system. However, it also includes advanced features like handling large file uploads and downloads, making it a more comprehensive solution for network and file system operations.
expo-file-system
Provides access to the local file system on the device.
API documentation
Installation
This package is pre-installed in managed Expo projects. You may skip the rest of the installation guide if this applies to you.
For bare React Native projects, this package is included in react-native-unimodules
, so if you use that then you already have this! If you are intentionally not using that package, follow the stpes below to install expo-file-system
manually.
Add the package to your npm dependencies
npm install expo-file-system
Configure for iOS
Run pod install
in the ios directory after installing the npm package.
Configure for Android
No additional set up necessary.
Contributing
Contributions are very welcome! Please refer to guidelines described in the contributing guide.