Native filesystem access for react-native
Go to file
Johannes Lumpe 45fabb05c0 Bumped version to 0.2.0 2015-05-08 20:21:48 +03:00
RNFS.xcodeproj Initial commit 2015-05-08 20:05:37 +03:00
.gitignore Initial commit 2015-05-08 20:05:37 +03:00
.jshintrc Initial commit 2015-05-08 20:05:37 +03:00
FS.android.js Initial commit 2015-05-08 20:05:37 +03:00
FS.ios.js Added Cache and Document directory paths as constants 2015-05-08 20:17:59 +03:00
LICENSE Initial commit 2015-05-08 20:05:37 +03:00
NSArray+Map.h Initial commit 2015-05-08 20:05:37 +03:00
NSArray+Map.m Initial commit 2015-05-08 20:05:37 +03:00
README.md Updated readme 2015-05-08 20:21:34 +03:00
RNFSManager.h Initial commit 2015-05-08 20:05:37 +03:00
RNFSManager.m Added Cache and Document directory paths as constants 2015-05-08 20:17:59 +03:00
package.json Bumped version to 0.2.0 2015-05-08 20:21:48 +03:00

README.md

react-native-fs

Native filesystem access for react-native

Note: this project is under development and functionality will improve over time. Currently it provides only the bare minimum of functionality.

Creating, renaming, copying, etc. of files will follow soon.

Usage

First you need to install react-native-fs:

npm install react-native-fs --save

In XCode, in the project navigator, right click Libraries ➜ Add Files to [your project's name] Go to node_modules ➜ react-native-keyboardevents and add the .xcodeproj file

In XCode, in the project navigator, select your project. Add the lib*.a from the RNFS project to your project's Build Phases ➜ Link Binary With Libraries Click .xcodeproj file you added before in the project navigator and go the Build Settings tab. Make sure 'All' is toggled on (instead of 'Basic'). Look for Header Search Paths and make sure it contains both (SRCROOT)/../react-native/React and (SRCROOT)/../../React - mark both as recursive.

Run your project (Cmd+R)

Examples

Basic

// require the module
var RNFS = require('react-native-fs');

// get a list of files and directories in the main bundle
RNFS.readDir('/', RNFS.MainBundle)
.then((result) => {
  console.log('GOT RESULT', result);

  // stat the first file
  return Promise.all([RNFS.stat(result[0].path), result[0].path]);
})
.then((statResult) => {
  if (statResult[0].isFile()) {
    // if we have a file, read it
    return RNFS.readFile(statResult[1]);
  }

  return 'no file';
})
.then((contents) => {
  // log the file contents
  console.log(contents);
})
.catch((err) => {
  console.log(err.message, err.code);
});

API

Constants

The following constants are available on the RNFS export:

MainBundle (Number) The identifier for the main bundle
CachesDirectory (Number) The identifier for the caches directory
DocumentDirectory (Number) The identifier for the document directory

CachesDirectoryPath (String) The absolute path to the caches directory DocumentDirectoryPath (String) The absolute path to the document directory

promise readDir(path, directory)

Reads the contents of path in directory.
path is a string and directory is one of the following:
RNFS.MainBundle, RNFS.CachesDirectory, RNFS.DocumentDirectory

The returned promise resolves with an array of objects with the following properties:

name (String), The name of the item
path (String), The absolute path to the item

promise stat(path)

Stats an item at path.
The promise resolves with an object with the following properties:
ctime (Date) - The creation date of the item mtime (Date) - The modification date of the item size (Number) - The size of the item in bytes
isFile (Function) - Returns true when the item is a file
isDirectory (Function) - Returns true when the item is a directory

promise readFile(path, shouldDecode)

Reads the file at path and - by default - decodes the transferred base64 string. If shouldDecode is false, the base64 encoded string is returned

Note: you will take quite a performance hit if you are reading big files