This repository is a mirror of the component module matthewp/fs. It has been modified to work with NPM+Browserify. You can install it using the command npm install npmcomponent/matthewp-fs
. Please do not open issues or send pull requests against this repo. If you have issues with this repo, report it to npmcomponent.
Bringing a file system abstraction to the browser. fs is a component 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 the FileSystem API (for Chrome) or IndexedDB (for Firefox/IE).
If using component
installation is easy:
$ component install matthewp/fs
If using AMD or Common JS just throw dist/fs-0.0.1.min.js
somewhere that your module loader can find it and use it.
If you don't like the module loaders you can still use fs by simply including it in your html:
<script src="fs-0.0.1.min.js"></script>
A single object will be attached to window
called fs, grab it and use it:
var fs = window.fs;
var fs = require('fs');
document.querySelector('input[type="file"]').addEventListener('change', function(e) {
var file = this.files[0]; // file is a File object.
fs.writeFile(file.name, file, function() {
// All done! File has been saved.
});
});
Saves the file data
with the name fileName
and calls the callback
. If an error is encountered, the first parameter the callback receives will be an Error
object.
Retrieves the file with the name fileName
and calls the callback
. The first parameter of the callback is an Error
object or null, the second parameter is the file's data as an ArrayBuffer
.
Retrieves the file with the name fileName
and calls the callback. The first parameter of the callback is an
Error`` or null, the second parameter is a string.
Removes the file with the name fileName
from storage and calls the callback
. The callback is called even if the file doesn't exist.
Gets the contents of fullPath
and calls the callback
. The callback will contain an array of DirectoryEntry
objects (see below).
Creates a directory at fullPath
and calls the callback
. The only parameter of the callback is an Error
, when applicable.
Removes the directory at fullPath
, recursively removing any files/subdirectories contained within.
A DirectoryEntry
object is passed to the callback of 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, callback)
. Throws a TypeError if the entry is not of type
file.