Skip to main content

filesystem

Reads and writes local files through the File System Access API's open/save pickers.

Import: @rtorcato/browser-common/filesystem

๐Ÿ“– MDN: File System Access API

The File System Access API is Chromium-only and pickFiles/saveFile must be called from a user gesture (e.g. a click handler) โ€” both throw a File System Access API not available error where unsupported, so guard with isFileSystemApiAvailable() first.

Browser supportโ€‹

APIChromeEdgeFirefoxSafariiOS SafariBaseline
Window.showOpenFilePickerโœ… 86+โœ… 86+โŒโŒโŒLimited
Window.showSaveFilePickerโœ… 86+โœ… 86+โŒโŒโŒLimited

โœ… supported ยท โ— partial ยท โŒ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: Window.showOpenFilePicker ยท Window.showSaveFilePicker

Exampleโ€‹

import {
isFileSystemApiAvailable,
pickFiles,
saveFile,
writeTextToFile,
} from '@rtorcato/browser-common/filesystem'

if (isFileSystemApiAvailable()) {
const files = await pickFiles({ multiple: true })

const stream = await saveFile({ suggestedName: 'notes.txt' })
await writeTextToFile(stream, 'hello')
}

Exportsโ€‹

  • isFileSystemApiAvailable() โ€” feature check
  • pickFiles(options?) โ€” prompts the user to select files, returns File[]
  • saveFile(options?) โ€” prompts the user to save a file, returns a writable file stream
  • readFileAsText(file) โ€” reads a File as text
  • readFileAsArrayBuffer(file) โ€” reads a File as an ArrayBuffer
  • writeTextToFile(stream, text) โ€” writes text to a writable stream and closes it
  • writeDataToFile(stream, data) โ€” writes a Blob/ArrayBuffer to a writable stream and closes it

See the API reference for full signatures.