Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

Classes

Kevin Cooper edited this page Jan 12, 2017 · 39 revisions

Index

RNFetchBlobConfig

A set of configurations that will be injected into a fetch method, with the following properties.

overwrite:boolean

0.10.0

When this property is true, the downloaded data will overwrite the existing file. (true by default)

// overwrite == true
RNFetchBlob.config({
    path : PATH_OF_AN_EXISTING_FILE,
    overwrite : true
})
.fetch('GET', 'http://www.example.com')
.then((res) => {
   // the file at path `PATH_OF_AN_EXISTING_FILE` will be overwrite by 
   // the response data from `http://www.example.com`
})

// overwrite == false
RNFetchBlob.config({
    path : PATH_OF_AN_EXISTING_FILE,
    overwrite : false
})
.fetch('GET', 'http://www.example.com')
.then((res) => {
   // the response data from `http://www.example.com` will append 
   // to the file at path PATH_OF_AN_EXISTING_FILE instead of replace it
})

timeout:number

0.8.0

Set timeout of the request (in milliseconds).

indicator:boolean

0.5.6

Set this property to true to display a network indicator on status bar, this feature is only supported on IOS.

trusty:boolean

0.5.3

Set this property to true will allow the request create connection with server have self-signed SSL certification. This is not recommended to use in production.

fileCache:boolean

Set this property to true will makes response data of the fetch stored in a temp file, by default the temp file will stored in App's own root folder with file name template RNFetchBlob_tmp${timestamp}.

appendExt:string

Set this property to change temp file extension that created by fetch response data.

path:string

When this property has value, fetch API will try to store response data in the path ignoring fileCache and appendExt property.

addAndroidDownloads:object (Android only)

This is an Android only property, it should be an object with the following properties :

  • useDownloadManager : download file using Android download manager or not.
  • title : title of the file
  • description : File description of the file.
  • path : The destination which the file will be downloaded, it SHOULD be a location on external storage (DCIMDir).
  • mime : MIME type of the file. By default is text/plain
  • mediaScannable : A boolean value, see [Officail Document](https://developer.android.com/reference/android/app/DownloadManager.html#addCompletedDownload(java.lang.String, java.lang.String, boolean, java.lang.String, java.lang.String, long, boolean))
  • notification : A boolean value decide whether show a notification when download complete.

RNFetchBlobResponse

When fetch success, it resolve a FetchBlobResponse object as first argument. FetchBlobResponse object has the following methods (these method are synchronous, so you might take quite a performance impact if the file is big)

base64():string

returns base64 string of response data (done in native context)

json():object

returns json parsed object (done in js context)

text():string

returns decoded base64 string (done in js context)

path():string

returns file path if the response data is cached in file

readFile(encoding:string):Promise

return a promise that resolves response data when possible.

readStream(encoding:string, bufferSize:number):Promise

return a promise that resolves a readStream object when possible.

session(name:string):RNFetchBlobSession

when the response data is cached in a file, this method adds the file into the session. The following usages are equivalent.

RNFetchBlob.session('session-name').add(resp.path())
// or
resp.session('session-name')

This method returns an object contains response information like status code, headers.

0.8.0

Response detail information object like status, headers.

RNFetchBlobResponseInfo

0.8.0

Properties

headers : any

An dictionary contains headers.

status : number

Status code of the response.

respType : 'text' | 'blob' | '' | 'son'

XMLHttpRequest response type.

rnfbEncode : 'path' | 'base64' | 'utf8'

RNFetchBlob.fetch response type, in RNFetchBlob.fetch response, when the response data is stored to file system, it should be path otherwise it depends on the response data, if the data can be encoded into UTF8 string it will be utf8 otherwise base64.

RNFetchBlobStat

0.5.0

Statistic data of a file, see the following sample object. This object usually comes from stat and lstat API

{
    // file name
    filename : 'foo.png',
    // folder of the file or the folder itself
    path : '/path/to/the/file/without/file/name/',
    // size, in bytes
    size : '4901',
    // `file` or `directory`
    type : 'file',
    // last modified timestamp
    lastModified : '141323298'
}

StatefulPromise

A class inherits Promise, it has extra method like progress, uploadProgress, and cancel which can help managing an asynchronous task's state.

Methods

cancel():Promise

0.8.0

Cancel the request when invoke this method.

progress((received, total) => void)

0.5.0

Add an event listener which triggers when data receiving from server.

progress(config, (received, total) => void)

0.9.6

Add an event listener with custom configuration, see usage.

uploadProgress((sent, total) => void)

0.8.0

Add an event listener which triggers when data chunks sent to server.

uploadProgress(config ,(sent, total) => void)

Add an event listener with custom configuration, see usage.

expire(() => void)

0.10.0

An IOS only API, when IOS app turns into background network tasks will be terminated after ~180 seconds, in order to handle these expired tasks, you can register an event handler, which will be called after the app become active.

RNFetchBlob.fetch('GET', 'http://example.com')
.expire(() => { console.log('task expired !!') })
.then(() => { ... })

RNFetchBlobReadStream

0.5.0

A file stream instance created by fs.readStream

Methods

open()

A file stream will NOT open automatically, invoke this method to start read chunks from the file.

onData((chunk:string | Array) => void)

Register an event handler handles the chunks read from the stream, when using ascii encoding chunk will be an Array contains number 0-255, otherwise it's a string.

onEnd(() => void)

Resiter an event handler which invoked when stream reaches end of file.

Clone this wiki locally