Editors, design tools and viewers need files. The web now has three levels of file support.
1. Open files from the OS (file_handlers)
Declare file types in the manifest, and your installed app appears in "Open with" menus:
"file_handlers": [
{ "action": "/open", "accept": { "text/markdown": [".md", ".markdown"] }, "launch_type": "single-client" }
]
Receive the files when the app launches:
if ('launchQueue' in window) {
launchQueue.setConsumer(async (params) => {
for (const handle of params.files) {
const file = await handle.getFile();
openDocument(await file.text(), handle);
}
});
}
Supported by Chrome and Edge on desktop for installed apps. The first time, the user is asked to allow it.
2. Pick, read and save files (File System Access API)
const [handle] = await window.showOpenFilePicker({ types: [{ description: 'Markdown', accept: { 'text/markdown': ['.md'] } }] });
const text = await (await handle.getFile()).text();
// ... edit ...
const writable = await handle.createWritable();
await writable.write(newText);
await writable.close(); // saved to the same file
showOpenFilePicker, showSaveFilePicker and showDirectoryPicker are Chromium desktop features. Use browser-fs-access to fall back to <input type="file"> and downloads in other browsers.
3. Private, fast storage (OPFS)
The Origin Private File System gives your app a private file system that users do not see, supported in all modern browsers. It is ideal for caches, large data and SQLite databases:
const root = await navigator.storage.getDirectory();
const fh = await root.getFileHandle('data.bin', { create: true });
const w = await fh.createWritable(); await w.write(bytes); await w.close();
In a worker, createSyncAccessHandle() gives very fast synchronous reads and writes; SQLite Wasm uses it.
Real-world examples: Excalidraw, Photopea and VS Code for the Web all open and save local files.
Compatibilidad de navegadores
| Función | Chrome / Edge desktop | Chrome Android | Safari iOS / iPadOS | Safari macOS | Firefox desktop | Firefox Android | Samsung Internet |
|---|---|---|---|---|---|---|---|
| File handlers | Chrome / Edge desktop: Sí | Chrome Android: No | Safari iOS / iPadOS: No | Safari macOS: No | Firefox desktop: No | Firefox Android: No | Samsung Internet: No |
| File System Access pickers | Chrome / Edge desktop: Sí | Chrome Android: No (OPFS only) | Safari iOS / iPadOS: No (OPFS only) | Safari macOS: No (OPFS only) | Firefox desktop: No (OPFS only) | Firefox Android: No | Samsung Internet: No |
| Origin Private File System | Chrome / Edge desktop: Sí | Chrome Android: Sí | Safari iOS / iPadOS: Sí | Safari macOS: Sí | Firefox desktop: Sí | Firefox Android: Sí | Samsung Internet: Sí |