🌐 You can configure expo-file-system using its built-in config plugin if you use config plugins in your project (Continuous Native Generation (CNG)). The plugin allows you to configure various properties that cannot be set at runtime and require building a new app binary to take effect. If your app does not use CNG, then you'll need to manually configure the library.
A boolean to enable LSSupportsOpeningDocumentsInPlace in Info.plist. This allows the app to open documents in place.
enableFileSharing
false
Only for:
iOS
A boolean to enable UIFileSharingEnabled in Info.plist. This enables file sharing in the iOS Files app, making the app's Documents directory accessible to users through the Files app, iTunes File Sharing, and other file management tools.
Are you using this library in an existing React Native app?
🌐 If you're not using Continuous Native Generation (CNG) or you're using native ios project manually, then you need to add the LSSupportsOpeningDocumentsInPlace and UIFileSharingEnabled keys to your project's ios/[app]/Info.plist:
🌐 The file or directory does not need to exist — an error will be thrown from the constructor only if the wrong class is used to represent an existing path (so if you try to create a File instance passing a path to an already existing directory).
特性
🌐 Features
同步和异步,对文件内容的读写访问
创建、修改和删除
可用属性,如 type、size、creationDate 等
能够以流的方式或使用 FileHandle 类读取和写入文件
使用 downloadFileAsync 或 expo/fetch 轻松下载/上传文件
使用平台原生流程预览文件
示例
🌐 Examples
写入和读取文本文件
example.ts
import{ File, Paths }from'expo-file-system';try{const file =newFile(Paths.cache,'example.txt');
file.create();// can throw an error if the file already exists or no permission to create itawait file.write('Hello, world!');// or `file.writeSync('Hello, world!');` for synchronous callconsole.log(file.textSync());// Hello, world!}catch(error){console.error(error);}
使用系统选择器选择文件
expo-document-picker 的用法:
🌐 Usage with expo-document-picker:
example.ts
import{ File }from'expo-file-system';import*as DocumentPicker from'expo-document-picker';try{const result =await DocumentPicker.getDocumentAsync({ copyToCacheDirectory:true});if(!result.canceled){const{ uri }= result.assets[0];const file =newFile(uri);console.log(file.textSync());}}catch(error){console.error(error);}
🌐 Use FileHandle for efficient, random-access reads of large files without loading the entire file into memory. Obtain a handle by calling file.open(), read or write at any position using the offset property, and always close the handle when finished.
example.ts
import{ File, Paths, FileMode }from'expo-file-system';const file =newFile(Paths.document,'recording.wav');const handle = file.open(FileMode.ReadOnly);// Read the WAV header (first 44 bytes)const header = handle.readBytesSync(44);const sampleRate =newDataView(header.buffer).getUint32(24,true);console.log(`Sample rate: ${sampleRate} Hz`);// Seek to a specific offset and read a chunk
handle.offset =1024;const chunk =await handle.readBytes(4096);console.log(`Read ${chunk.length} bytes from offset 1024`);// Read an entire file in 64 KB chunks
handle.offset =0;constCHUNK_SIZE=64*1024;while(handle.offset!< handle.size!){const data =await handle.readBytes(CHUNK_SIZE);// Process data...}
handle.close();