提供对设备上本地文件系统的访问的库。
next
版本的 FileSystem API 包含在expo-file-system
库中。它可以与以前的 API 一起使用,并提供一种简化的面向对象的方式来执行文件系统操作。
expo-file-system/next
提供对设备本地存储的文件系统的访问。它还可以从网络下载文件。
¥expo-file-system/next
provides access to the file system stored locally on the device. It can also download files from the network.
为了提供更快的更新,Expo Go 和 Snack 目前不支持expo-file-system/next
。要使用它,请创建一个 开发构建。
¥Installation
-
npx expo install expo-file-system
If you are installing this in an existing React Native app, start by installing expo
in your project. Then, follow the additional instructions as mentioned by the library's README under "Installation in bare React Native projects" section.
¥Usage
¥Writing and reading text files
import { File, Paths } from 'expo-file-system/next';
try {
const file = new File(Paths.cache, 'example.txt');
file.create(); // can throw an error if the file already exists or no permission to create it
file.write('Hello, world!');
console.log(file.text()); // Hello, world!
} catch (error) {
console.error(error);
}
¥Downloading files
import { File, Paths } from 'expo-file-system/next';
const url = 'https://pdfobject.com/pdf/sample.pdf';
const destination = new Directory(Paths.cache, 'pdfs');
try {
destination.create();
const output = await File.downloadFileAsync(url, destination);
console.log(output.exists); // true
console.log(output.uri); // path to the downloaded file, e.g. '${cacheDirectory}/pdfs/sample.pdf'
} catch (error) {
console.error(error);
}
¥Moving and copying files
import { File, Paths } from 'expo-file-system/next';
try {
const file = new File(Paths.document, 'example.txt');
file.create();
console.log(file.uri); // '${documentDirectory}/example.txt'
file.move(Paths.cache);
console.log(file.uri); // '${cacheDirectory}/example.txt'
file.move(new Directory(Paths.cache, 'newFolder'));
console.log(file.uri); // '${cacheDirectory}/newFolder/example.txt'
} catch (error) {
console.error(error);
}
¥Using legacy FileSystem API
import * as FileSystem from 'expo-file-system';
import { File, Paths } from 'expo-file-system/next';
try {
const file = new File(Paths.cache, 'example.txt');
const content = await FileSystem.readAsStringAsync(file.uri);
console.log(content);
} catch (error) {
console.error(error);
}
¥Listing directory contents recursively
import { Directory, Paths } from 'expo-file-system/next';
function printDirectory(directory: Directory, indent: number = 0) {
console.log(`${' '.repeat(indent)} + ${directory.name}`);
const contents = directory.list();
for (const item of contents) {
if (item instanceof Directory) {
printDirectory(item, indent + 2);
} else {
console.log(`${' '.repeat(indent + 2)} - ${item.name} (${item.size} bytes)`);
}
}
}
try {
printDirectory(new Directory(Paths.cache));
} catch (error) {
console.error(error);
}
Type: Class extends FileSystemDirectory
Represents a directory on the filesystem.
A Directory
instance can be created for any path, and does not need to exist on the filesystem during creation.
Directory Properties
boolean
A boolean representing if a directory exists. true
if the directory exists, false
otherwise.
Also false
if the application does not have read access to the file.
string
Represents the directory URI. The field is read-only, but it may change as a result of calling some methods such as move
.
Directory Methods
Copies a directory.
any
Deletes a directory. Also deletes all files and directories inside the directory.
void
Type: Class extends FileSystemFile
File Properties
boolean
A boolean representing if a file exists. true
if the file exists, false
otherwise.
Also false
if the application does not have read access to the file.
null | string
An md5 hash of the file. Null if the file does not exist or it cannot be read.
null | number
A size of the file in bytes. Null if the file does not exist or it cannot be read.
string
Represents the file URI. The field is read-only, but it may change as a result of calling some methods such as move
.
File Methods
Retrieves content of the file as base64.
string
The contents of the file as a base64 string.
Retrieves byte content of the entire file.
Uint8Array
The contents of the file as a Uint8Array.
Parameter | Type | Description |
---|---|---|
url | string | The URL of the file to download. |
destination | Directory | File | The destination directory or file. If a directory is provided, the resulting filename will be determined based on the response headers. |
Moves a directory. Updates the uri
property that now points to the new location.
any
Returns a FileHandle object that can be used to read and write data to the file.
FileHandle
ReadableStream<Uint8Array>
WritableStream<Uint8Array>
Parameter | Type | Description |
---|---|---|
content | string | Uint8Array | The content to write into the file. |
Writes content to the file.
void
Type: Class extends PathUtilities
Paths Properties
Record<string, Directory>
Directory
A property containing the cache directory – a place to store files that can be deleted by the system when the device runs low on storage.
Directory
A property containing the document directory – a place to store files that are safe from being deleted by the system.
Paths Methods
Parameter | Type | Description |
---|---|---|
path | string | File | Directory | The path to get the base name from. |
ext(optional) | string | An optional file extension. |
Returns the base name of a path.
string
A string representing the base name.
Returns the directory name of a path.
string
A string representing the directory name.
Returns the extension of a path.
string
A string representing the extension.
Checks if a path is absolute.
boolean
true
if the path is absolute, false
otherwise.
Joins path segments into a single path.
string
A string representing the joined path.
Normalizes a path.
string
A string representing the normalized path.
Parses a path into its components.
{
base: string,
dir: string,
ext: string,
name: string,
root: string
}
An object containing the parsed path components.