fs

File system abstraction layer for the browser

Github stars Tracking Chart

fs-web

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.

Installation

Install via npm:

npm install fs-web --save

Example

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} ]
  });

API

All methods return a Promise.

fs.writeFile(fileName, data)

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.

fs.readFile(fileName)

Retrieves the file with the name fileName and returns a Promise. The Promise will resolve with the file's data as an ArrayBuffer.

fs.readString(fileName)

Retrieves the file with the name fileName and returns a Promise. The Promise will resolve with a string representation of fileName.

fs.removeFile(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.

fs.readdir(fullPath)

Gets the contents of fullPath and returns a Promise. The Promise will resolve with an array of DirectoryEntry objects (see below).

fs.mkdir(fullPath)

Creates a directory at fullPath and returns a Promise.

fs.rmdir(fullPath)

Removes the directory at fullPath, recursively removing any files/subdirectories contained within. Returns a Promise that will resolve when the fullPath is removed.

DirectoryEntry

A DirectoryEntry object is resolved from fs.readdir and represents either a file or a directory. A DirectoryEntry instance contains these properties/methods:

DirectoryEntry#path

The path property is the full path (including file name) for the given file/directory entry.

DirectoryEntry#name

The name of the given entry, either the file or directory name.

DirectoryEntry#dir

The given directory that the file/directory sits in.

DirectoryEntry#type

The type of the entry, either file or directory.

DirectoryEntry#readFile()

A convenience method for calling readFile(fileName). Throws a TypeError if the entry is not of type file.

License

BSD 2 Clause

Main metrics

Overview
Name With Ownermatthewp/fs
Primary LanguageJavaScript
Program languageJavaScript (Language Count: 2)
Platform
License:
所有者活动
Created At2013-01-13 18:16:43
Pushed At2023-02-23 20:04:14
Last Commit At2016-01-27 11:51:54
Release Count6
Last Release Namev1.0.1 (Posted on 2016-01-27 11:52:47)
First Release Name0.0.2 (Posted on 2013-11-26 08:02:47)
用户参与
Stargazers Count163
Watchers Count11
Fork Count13
Commits Count50
Has Issues Enabled
Issues Count9
Issue Open Count4
Pull Requests Count6
Pull Requests Open Count1
Pull Requests Close Count0
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private