Classes - wkh237/react-native-fetch-blob GitHub Wiki

Index

RNFetchBlobAndroidDownloadOption

0.10.6

Option object for Android Download manager API like android.addCompleteDownload

title:string

Title string to be displayed when the file added to Downloads app.

description:string

File description to be displayed when the file added to Downloads app.

mime:string

MIME string of the file.

path:string

URI string of the file.

showNotification:boolean

Boolean value that determines if notification will be displayed.

RNFetchBlobConfig

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

IOSBackgroundTask:boolean

0.10.6

Set this flag to true if you wish the request can last as long as possible even the app entering background. Notice that when enable background task, the timeout does not take effect, also the redirect record will be empty. See #368

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.

Notice : Android Download Manager only supports GET requests

followRedirect:boolean (detault = true)

0.10.2-beta.6

Property represents if it automatically follow redirects, default value is true.

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<RNFetchBlobSession>

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

info(): RNFetchBlobResponseInfo

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 methods like progress, uploadProgress, and cancel which can help managing an asynchronous task's state.

Methods

cancel(): StatefulPromise

0.8.0

Cancel the request when invoking this method.

stateChange(serverResponse => void): StatefulPromise

0.9.0

Add an event listener which will be triggered when StatefulPromise's state changing.

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

onError((detail) => 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)

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

RNFetchBlobWriteStream

0.5.0

A file stream instance created by fs.writeStream

Methods

write(data: string | Array<number>): Promise<void>

Writes the given data to the stream. If the encoding is "ascii" the data is expected to be an array of 0..255 numbers, otherwise a string (encoding "utf8" and "base64").

close(): Promise<void>

Closes the stream.