Giter Club home page Giter Club logo

matthewp-fs's Introduction

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.

fs

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).

Installation

Component

If using component installation is easy:

$ component install matthewp/fs

AMD / Common JS

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.

All others

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;

Example

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

API

fs.writeFile(fileName, data, callback)

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.

fs.readFile(fileName, callback)

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.

fs.readString(fileName, callback)

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.

fs.removeFile(fileName, callback)

Removes the file with the name fileName from storage and calls the callback. The callback is called even if the file doesn't exist.

fs.readdir(fullPath, callback)

Gets the contents of fullPath and calls the callback. The callback will contain an array of DirectoryEntry objects (see below).

fs.mkdir(fullPath, callback)

Creates a directory at fullPath and calls the callback. The only parameter of the callback is an Error, when applicable.

fs.rmdir(fullPath, callback)

Removes the directory at fullPath, recursively removing any files/subdirectories contained within.

DirectoryEntry

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:

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(callback)

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

matthewp-fs's People

Contributors

airportyh avatar coderaiser avatar matthewp avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.