Selaa lähdekoodia

Update README.md

wkh237 9 vuotta sitten
vanhempi
commit
dbe2015233
1 muutettua tiedostoa jossa 67 lisäystä ja 8 poistoa
  1. 67
    8
      README.md

+ 67
- 8
README.md Näytä tiedosto

340
 
340
 
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**.
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
 ```js
345
 ```js
346
 let data = ''
346
 let data = ''
347
 let ifstream = RNFetchBlob.readStream(
347
 let ifstream = RNFetchBlob.readStream(
348
-    // encoding, should be one of `base64`, `utf8`
348
+    // encoding, should be one of `base64`, `utf8`, `ascii`
349
     'base64',
349
     'base64',
350
     // file path
350
     // file path
351
     PATH_TO_THE_FILE,
351
     PATH_TO_THE_FILE,
353
     // when reading file in BASE64 encoding, buffer size must be multiples of 3.
353
     // when reading file in BASE64 encoding, buffer size must be multiples of 3.
354
     4095)
354
     4095)
355
 ifstream.onData((chunk) => {
355
 ifstream.onData((chunk) => {
356
+  // when encoding is `ascii`, chunk will be an array contains numbers
357
+  // otherwise it will be a string
356
   data += chunk
358
   data += chunk
357
 })
359
 })
358
 ifstream.onError((err) => {
360
 ifstream.onError((err) => {
443
 
445
 
444
 Config API was introduced in `v0.5.0` which provides some options for the `fetch` task. 
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
 #### `fetch(method, url, headers, body):Promise<FetchBlobResponse>`
450
 #### `fetch(method, url, headers, body):Promise<FetchBlobResponse>`
447
 
451
 
448
 `legacy`
452
 `legacy`
488
 
492
 
489
 `0.5.0`
493
 `0.5.0`
490
 
494
 
491
-#### `getSystemDirs():Promise<object>`
495
+#### getSystemDirs():Promise<object>
492
 
496
 
493
 This method returns common used folders:
497
 This method returns common used folders:
494
 
498
 
510
 
514
 
511
 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).
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
 #### writeStream(path:string, encoding:string, append:boolean):Promise<WriteStream>
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
 #### mkdir(path:string):Promise
584
 #### mkdir(path:string):Promise
526
 
585