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

Commit dbe2015

Browse files
committed
Update README.md
1 parent a156e4e commit dbe2015

File tree

1 file changed

+67
-8
lines changed

1 file changed

+67
-8
lines changed

README.md

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -340,19 +340,21 @@ See [fs](#user-content-fs) chapter for more information
340340

341341
In `v0.5.0` we've added `writeStream` and `readStream`, which allows your app read/write data from file path. This API creates a file stream, rather than convert whole data into BASE64 encoded string, it's handy when processing **large files**.
342342

343-
But there're some differences between `readStream` and `writeStream` API. When calling `readStream` method, the file stream is opened immediately, and start to read data.
343+
When calling `readStream` method, you have to `open` the stream, and start to read data.
344344

345345
```js
346346
let data = ''
347347
let ifstream = RNFetchBlob.readStream(
348-
// encoding, should be one of `base64`, `utf8`
348+
// encoding, should be one of `base64`, `utf8`, `ascii`
349349
'base64',
350350
// file path
351351
PATH_TO_THE_FILE,
352352
// (optional) buffer size, default to 4096 (4095 for BASE64 encoded data)
353353
// when reading file in BASE64 encoding, buffer size must be multiples of 3.
354354
4095)
355355
ifstream.onData((chunk) => {
356+
// when encoding is `ascii`, chunk will be an array contains numbers
357+
// otherwise it will be a string
356358
data += chunk
357359
})
358360
ifstream.onError((err) => {
@@ -443,6 +445,8 @@ You can also group the requests by using `session` API, and use `dispose` to rem
443445

444446
Config API was introduced in `v0.5.0` which provides some options for the `fetch` task.
445447

448+
see [RNFetchBlobConfig](#user-content-rnfetchblobconfig)
449+
446450
#### `fetch(method, url, headers, body):Promise<FetchBlobResponse>`
447451

448452
`legacy`
@@ -488,7 +492,7 @@ RNFetchBlob.base64.decode(data)
488492

489493
`0.5.0`
490494

491-
#### `getSystemDirs():Promise<object>`
495+
#### getSystemDirs():Promise<object>
492496

493497
This method returns common used folders:
494498

@@ -510,17 +514,72 @@ RNFetchBlob.getSystemDirs().then((dirs) => {
510514

511515
If you're going to make downloaded file visible in Android `Downloads` app, please see [Show Downloaded File and Notification in Android Downloads App](#user-content-show-downloaded-file-in-android-downloads-app).
512516

513-
#### createFile(path:string, data:string, encoding:string ):Promise
517+
#### createFile(path, data, encoding):Promise
514518

515-
TODO
519+
#### path:`string`
520+
The path which this new file will be created.
521+
#### data:`string` | `Array<number>`
522+
Content of the new file, when `encoding` is `ascii`, this argument shoud be an array contains number 0~255.
523+
#### encoding:`utf8` | `base64` | `ascii`
524+
Encoding of content.
525+
526+
the following expressions are equivalent.
527+
528+
```js
529+
const fs = RNFetchBlob.fs
530+
const base64 = RNFetchBlob.base64
531+
fs.createFile(NEW_FILE_PATH, 'foo', 'utf8')
532+
fs.createFile(NEW_FILE_PATH, [102, 111, 111], 'ascii')
533+
fs.createFile(NEW_FILE_PATH, base64.encode('foo'), 'base64')
534+
```
516535

517536
#### writeStream(path:string, encoding:string, append:boolean):Promise<WriteStream>
518537

519-
TODO
538+
#### path:`string`
539+
The path to the file the stream is writing to.
540+
#### encoding:`utf8` | `base64` | `ascii`
541+
Encoding of input data.
542+
#### append:`boolean`(optional, default to `false`)
543+
Will new data append after existing file or not.
520544

521-
#### readStream(path:string, encoding:string, append:boolean):Promise<ReadStream>
545+
Calling `writeStream` method will returns a Promise, which resolves a `RNFetchBlobWriteSteam` instance when stream opened successfully.
546+
547+
```js
548+
// write utf8 data
549+
RNFetchBlob.fs.writeStream(PATH_TO_WRITE, 'utf8')
550+
.then((stream) => {
551+
stream.write('foo')
552+
return stream.close()
553+
})
554+
// write ASCII data
555+
RNFetchBlob.fs.writeStream(PATH_TO_WRITE, 'ascii')
556+
.then((stream) => {
557+
// write char `f`
558+
stream.write([102])
559+
// write char `o`, `o`
560+
stream.write([111,111])
561+
return stream.close()
562+
})
563+
// write BASE64
564+
RNFetchBlob.fs.writeStream(PATH_TO_WRITE, 'base64')
565+
.then((stream) => {
566+
stream.write(RNFetchBlob.base64.encode('foo'))
567+
return stream.close()
568+
})
569+
570+
```
571+
572+
#### readStream(path, encoding, bufferSize):Promise<ReadStream>
573+
574+
#### path:`string`
575+
The path to the file the stream is reading from.
576+
#### encoding:`string`
577+
Encoding of the data.
578+
#### bufferSize:`number`(optional)
579+
Buffer size of read stream, default to `4096` and `4095`(when encoding is `base64`)
580+
581+
`readStream` returns a promise which will resolve `RNFetchBlobReadStream`.
522582

523-
TODO
524583

525584
#### mkdir(path:string):Promise
526585

0 commit comments

Comments
 (0)