Skip to content

File System Access API

wkh237 edited this page Jul 8, 2016 · 36 revisions

RNFetchBlob.fs

Notice

Accessing files in camera roll and assets are currently not supported, we are now working on this issue

File System API was introduced in 0.5.0, including the following methods

dirs

This constant is a hash map contains common used folders:

  • DocumentDir
  • CacheDir
  • DCIMDir (Android Only)
  • DownloadDir (Android Only)
  • MusicDir (Android Only)
  • PictureDir (Android Only)
  • MovieDir (Android Only)
  • RingtoneDir (Android Only)
const dirs = RNFetchBlob.fs.dirs
console.log(dirs.DocumentDir)
console.log(dirs.CacheDir)
console.log(dirs.DCIMDir)
console.log(dirs.DownloadDir)

If you're going to make downloaded file visible in Android Downloads app, please see Show Downloaded File and Notification in Android Downloads App.

createFile(path, data, encoding):Promise

path:string

The path which this new file will be created.

data:string | Array<number>

Content of the new file, when encoding is ascii, this argument shoud be an array contains number 0~255.

encoding:utf8 | base64 | ascii

Encoding of content.

the following expressions are equivalent.

const fs = RNFetchBlob.fs
const base64 = RNFetchBlob.base64
fs.createFile(NEW_FILE_PATH, 'foo', 'utf8')
fs.createFile(NEW_FILE_PATH, [102, 111, 111], 'ascii')
fs.createFile(NEW_FILE_PATH, base64.encode('foo'), 'base64')

writeFile(path:string, content:string | Array, encoding:string):Promise

0.6.0

path:string

The path of the file to write.

content:string | Array<number>

Data that write to the path, should be an utf8/base64 encoded string, or an array contains numbers between 0-255.

encoding:utf8 | base64 | ascii

Encoding of input data.

writeFile API replaces content in the file, if you're going to append data to existing file, you should use appendFile or writeStream.

// write UTF8 data to file
RNFetchBlob.fs.writeFile(PATH_TO_WRITE, 'foo', 'utf8')
              .then(()=>{ ... })
// write bytes to file
RNFetchBlob.fs.writeFile(PATH_TO_WRITE, [102,111,111], 'ascii')
              .then(()=>{ ... })
// write base64 data to file
RNFetchBlob.fs.writeFile(PATH_TO_WRITE, RNFetchBlob.base64.encode('foo'), 'base64')
              .then(()=>{ ... })

// the file should have content
// foo

writeStream(path:string, encoding:string):Promise

0.5.0

path:string

The path to the file the stream is writing to.

encoding:utf8 | base64 | ascii

Encoding of input data.

append:boolean(optional, default to false)

Will new data append after existing file or not.

Calling writeStream method will returns a Promise, which resolves a RNFetchBlobWriteSteam instance when stream opened successfully.

// write utf8 data
RNFetchBlob.fs.writeStream(PATH_TO_WRITE, 'utf8')
    .then((stream) => {
        stream.write('foo')
        return stream.close()
    })
// write ASCII data
RNFetchBlob.fs.writeStream(PATH_TO_WRITE, 'ascii')
    .then((stream) => {
        // write char `f`
        stream.write([102])
        // write char `o`, `o`
        stream.write([111,111])
        return stream.close()
    })
// write BASE64
RNFetchBlob.fs.writeStream(PATH_TO_WRITE, 'base64')
    .then((stream) => {
        stream.write(RNFetchBlob.base64.encode('foo'))
        return stream.close()
    })

appendFile(path:string, content:string | Array, encoding:string):Promise

0.6.0

path:string

The path of the file to write.

content:string | Array<number>

Data that write to the path, should be an utf8/base64 encoded string, or an array contains numbers between 0-255.

encoding:utf8 | base64 | ascii

Encoding of input data.

appendFile is will append content after existing data, if you're going to overwrite file content, you should use writeFile or writeStream.

// write UTF8 data to file
RNFetchBlob.fs.appendFile(PATH_TO_WRITE, 'foo', 'utf8')
              .then(()=>{ ... })
// write bytes to file
RNFetchBlob.fs.appendFile(PATH_TO_WRITE, [102,111,111], 'ascii')
              .then(()=>{ ... })
// write base64 data to file
RNFetchBlob.fs.appendFile(PATH_TO_WRITE, RNFetchBlob.base64.encode('foo'), 'base64')
              .then(()=>{ ... })

// the file should have content
// foofoofoo

readFile(path, encoding):Promise

0.6.0

path:string

Path of the file to file.

encoding:string

Decoder to decode the file data, should be one of base64, ascii, and utf8, it uses utf8 by default.

Read the file from the given path, if the file is large, you should consider use readStream instead.

RNFetchBlob.fs.readFile(PATH_TO_READ, 'base64')
.then((data) => {
  // handle the data ..
})

readStream(path, encoding, bufferSize):Promise

0.5.0

path:string

The path to the file the stream is reading from.

encoding:string

Encoding of the data.

bufferSize:number(optional)

Buffer size of read stream, default to 4096 and 4095(when encoding is base64)

readStream returns a promise which will resolve RNFetchBlobReadStream.

RNFetchBlob.fs.readStream(PATH_TO_READ, 'utf8')
    .then((stream) => {
        let data = ''
        stream.open()
        stream.onData((chunk) => {
            chunk += data
        })
        stream.onEnd(() => {
            console.log(data)
        })
    })

mkdir(path:string):Promise

0.5.0

Create a directory named path

RNFetchBlob.fs.mkdir(PATH_TO_CREATE)
.then(() => { ... })
.catch((err) => { ... })

ls(path:string):Promise<Array>

0.5.0

List files and directories in a path

RNFetchBlob.fs.ls(PATH_TO_LIST)
    // files will an array contains filenames
    .then((files) => {
        console.log(files)
    })

mv(from:string, to:string):Promise

0.5.0

Move a file's location

RNFetchBlob.fs.mv(FROM_PATH, TO_PATH)
.then(() => { ... })
.catch(() => { ... })

cp(src:string, dest:string):Promise

Copy a file.

RNFetchBlob.fs.mv(SRC_PATH, DEST_PATH)
.then(() => { ... })
.catch(() => { ... })

exists(path:string):Promise

0.5.0

Check if a file exist at path

RNFetchBlob.fs.exists(PATH_OF_FILE)
.then((exist) => {
    console.log(`file ${exist ? '' : 'not'} exists`)
})
.catch(() => { ... })

isDir(path:string):Promise

Check the file at path is a directory or not. Resolves with false when the path is not a directory, or it does not exists.

RNFetchBlob.fs.exists(PATH_OF_FILE)
.then((isDir) => {
    console.log(`file is ${isDir ? '' : 'not'} a directory`)
})

unlink(path:string):Promise

0.5.0

Delete a file at path

RNFetchBlob.fs.unlink(path)
.then(() => { ... })
.catch((err) => { ... })

lstat(path:string):Promise

0.5.0

Get statistic data of files in a directory, the result data will be an array of RNFetchBlobStat object.

RNFetchBlob.fs.lstat(PATH_OF_A_FOLDER)
    .then((stats) => {})
    .catch((err) => {})

stat(path:string):Promise

0.5.0

Similar get statistic a data or a directory. the result data will be a RNFetchBlobStat object.

RNFetchBlob.fs.stat(PATH_OF_THE_TARGET)
    .then((stats) => {})
    .catch((err) => {})

scanFile(path:string):Promise (Androi Only)

0.5.1

Connect Media Scanner and scan the file. see Android Media Scanner, and Downloads App Support chapter for more information.

Work In Progress function 0.6.2

The following APIs are still work in progress, you can not use them in any existing release

asset(filename:string):string

0.6.2

When the file comes from app bundle, you should use this method to prepend a prefix bundle-assets:// so that the fs APIs know this is a file in app bundle.

let path = RNFetchBlob.fs.asset('my-asset.jpg')

Restrictions on assets

Assets files are compiled into the app bundle so generally they're readonly, currently only the following operations work on asset files.

  • cp
  • readFile
  • readStream
  • stat
Clone this wiki locally