Bringing a file system abstraction to the browser. fs is a module that allows you to store data in the (modern) browser using an API similar to that of Node's fs module
Implemented in a cross-browser fashion, using IndexedDB.
Install via npm:
npm install fs-web --save
Writing from a file input.
import { writeFile } from 'fs-web';
let input = document.querySelector('input[type="file"]');
input.addEventListener('change', function(e) {
let file = this.files[0]; // file is a File object.
writeFile(file.name, file).then(function() {
// All done! File has been saved.
});
});
Writing and reading.
import * as fs from 'fs-web';
fs.writeFile('foo/some-file.txt', 'foo')
.then(function(){
return fs.readdir('foo');
})
.then(function(files){
files // -> [ {some-file.txt} ]
});
All methods return a Promise.
Saves the file data
with the name fileName
and returns a Promise. If an error is encountered, the Promise will be rejected with an Error
object.
Retrieves the file with the name fileName
and returns a Promise. The Promise will resolve with the file's data as an ArrayBuffer
.
Retrieves the file with the name fileName
and returns a Promise. The Promise will resolve with a string representation of fileName
.
Removes the file with the name fileName
from storage and returns a Promise. The Promise will resolve even if the fileName doesn't exist.
Gets the contents of fullPath
and returns a Promise. The Promise will resolve with an array of DirectoryEntry
objects (see below).
Creates a directory at fullPath
and returns a Promise.
Removes the directory at fullPath
, recursively removing any files/subdirectories contained within. Returns a Promise that will resolve when the fullPath is removed.
A DirectoryEntry
object is resolved from fs.readdir
and represents either a file or a directory. A DirectoryEntry instance contains these properties/methods:
The path
property is the full path (including file name) for the given file/directory entry.
The name
of the given entry, either the file or directory name.
The given directory that the file/directory sits in.
The type
of the entry, either file or directory.
A convenience method for calling readFile(fileName)
. Throws a TypeError if the entry is not of type
file.
BSD 2 Clause