Browser-NativeFS
This module allows you to easily use the
Native File System API on supporting browsers,
with a transparent fallback to the <input type="file">
and <a download>
legacy methods.
Read more on the background of this module in my post
Progressive Enhancement In the Age of Fugu APIs.
Usage Example
The module feature-detects support for the Native File System API and
only loads the actually relevant code.
import {
fileOpen,
fileSave,
} from 'https://unpkg.com/browser-nativefs';
(async () => {
const blob = await fileOpen({
mimeTypes: ['image/*'],
});
const blobs = await fileOpen({
mimeTypes: ['image/*'],
multiple: true,
});
await fileSave(blob, {
fileName: 'Untitled.png',
});
})();
API Documentation
Opening files:
const options = {
mimeTypes: ['image/*'],
extensions: ['png', 'jpg', 'jpeg', 'webp'],
multiple: true,
description: 'Image files',
};
const blobs = await fileOpen(options);
Saving files:
const options = {
fileName: 'Untitled.txt',
};
const handle = previouslyOpenedBlob.handle;
await fileSave(someBlob, options, handle);
Browser-NativeFS in Action
You can see the module in action in the Excalidraw drawing app.
Acknowledgements
Thanks to @developit
for improving the dynamic module loading.
License
Apache 2.0.