Skip to main content
Glama
filesystem-next.md3.84 kB
--- title: FileSystem (next) description: A library that provides access to the local file system on the device. sourceCodeUrl: https://github.com/expo/expo/tree/sdk-53/packages/expo-file-system/next packageName: expo-file-system iconUrl: /static/images/packages/expo-file-system.png platforms: ["android", "ios", "tvos"] --- > **info** The `next` version of the FileSystem API is included in the `expo-file-system` library. It can be used alongside the previous API, and offers a simplified, object oriented way of performing filesystem operations. > **important** To provide quicker updates, `expo-file-system/next` is currently unsupported in Expo Go and Snack. To use it, create a [development build](/develop/development-builds/create-a-build/). `expo-file-system/next` provides access to the file system stored locally on the device. It can also download files from the network. ## Installation ## Usage ### Writing and reading text files ```ts example.ts 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 Using `downloadFileAsync`: ```ts example.ts 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); } ``` Or using `expo/fetch`: ```ts example.ts const url = 'https://pdfobject.com/pdf/sample.pdf'; const response = await fetch(url); const src = new File(testDirectory, 'file.pdf'); src.write(await response.bytes()); ``` ### Uploading files using `expo/fetch` You can upload files as blobs directly with `fetch` built into the Expo package: ```ts example.ts const src = new File(testDirectory, 'file.txt'); file.write('Hello, world!'); const blob = src.blob(); const response = await fetch('https://example.com', { method: 'POST', body: blob, }); ``` Or using the `FormData` constructor: ```ts example.ts const src = new File(testDirectory, 'file.txt'); file.write('Hello, world!'); const blob = src.blob(); const formData = new FormData(); formData.append('data', blob); const response = await fetch('https://example.com', { method: 'POST', body: formData, }); ``` ### Moving and copying files ```ts example.ts 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 ```ts example.ts 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 ```ts example.ts 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); } ``` ## API

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jaksm/expo-docs-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server