Browse Source

Update README.md

wkh237 8 years ago
parent
commit
dbe2015233
1 changed files with 67 additions and 8 deletions
  1. 67
    8
      README.md

+ 67
- 8
README.md View File

@@ -340,12 +340,12 @@ See [fs](#user-content-fs) chapter for more information
340 340
 
341 341
 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**.
342 342
 
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. 
344 344
 
345 345
 ```js
346 346
 let data = ''
347 347
 let ifstream = RNFetchBlob.readStream(
348
-    // encoding, should be one of `base64`, `utf8`
348
+    // encoding, should be one of `base64`, `utf8`, `ascii`
349 349
     'base64',
350 350
     // file path
351 351
     PATH_TO_THE_FILE,
@@ -353,6 +353,8 @@ let ifstream = RNFetchBlob.readStream(
353 353
     // when reading file in BASE64 encoding, buffer size must be multiples of 3.
354 354
     4095)
355 355
 ifstream.onData((chunk) => {
356
+  // when encoding is `ascii`, chunk will be an array contains numbers
357
+  // otherwise it will be a string
356 358
   data += chunk
357 359
 })
358 360
 ifstream.onError((err) => {
@@ -443,6 +445,8 @@ You can also group the requests by using `session` API, and use `dispose` to rem
443 445
 
444 446
 Config API was introduced in `v0.5.0` which provides some options for the `fetch` task. 
445 447
 
448
+see [RNFetchBlobConfig](#user-content-rnfetchblobconfig)
449
+
446 450
 #### `fetch(method, url, headers, body):Promise<FetchBlobResponse>`
447 451
 
448 452
 `legacy`
@@ -488,7 +492,7 @@ RNFetchBlob.base64.decode(data)
488 492
 
489 493
 `0.5.0`
490 494
 
491
-#### `getSystemDirs():Promise<object>`
495
+#### getSystemDirs():Promise<object>
492 496
 
493 497
 This method returns common used folders:
494 498
 
@@ -510,17 +514,72 @@ RNFetchBlob.getSystemDirs().then((dirs) => {
510 514
 
511 515
 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).
512 516
 
513
-#### createFile(path:string, data:string, encoding:string ):Promise
517
+#### createFile(path, data, encoding):Promise
514 518
 
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
+```
516 535
 
517 536
 #### writeStream(path:string, encoding:string, append:boolean):Promise<WriteStream>
518 537
 
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.
520 544
 
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`.
522 582
 
523
-TODO
524 583
 
525 584
 #### mkdir(path:string):Promise
526 585