opfs
is a thin wrapper around OPFS providing high level functions to interact with the private file system.
npm config set @mickaelvieira:registry https://npm.pkg.github.com
yarn add @mickaelvieira/opfs
or
npm -i @mickaelvieira/opfs
Writing from an input file.
import * as fs from '@mickaelvieira/opfs';
const input = document.querySelector('input[type="file"]');
input.addEventListener('change', function(event: Event) {
for (const file of this.files) {
const handle = await fs.writeFile(file.name, file);
}
});
Writing from a string.
import * as fs from '@mickaelvieira/opfs';
const file = await fs.writeFile('foo/bar.txt', 'This is a string');
const entries = await fs.readDir('foo');
console.log(entries);
// Map(1) {'foo/bar.txt' => FileSystemFileHandle}
Removing a file.
import * as fs from '@mickaelvieira/opfs';
const result = await fs.removeFile('foo/bar.txt')
Creating a directory.
import * as fs from '@mickaelvieira/opfs';
const dir = await fs.mkdir('foo/bar')
const entries = await fs.readdir(dir);
console.log(entries); // Map(0)
Removing a directory.
import * as fs from '@mickaelvieira/opfs';
const result = await fs.rmdir('foo/bar')