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 in managed Expo projects
For managed managed Expo projects, please follow the installation instructions in the API documentation for the latest stable release. If you follow the link and there is no documentation available then this library is not yet usable within managed projects β it is likely to be included in an upcoming Expo SDK release.
Installation in bare React Native projects
For bare React Native projects, this package is included in react-native-unimodules
. Please refer to those installation instructions to install this package.
Contributing
Contributions are very welcome! Please refer to guidelines described in the contributing guide.