|
@@ -1,15 +1,3 @@
|
1
|
|
-## New Maintainers
|
2
|
|
-
|
3
|
|
-We make quite a bit of use of react-native-fetch-blob at Jolt and would like to maintain the project. Feel free to open issues, PRs, etc. here as you would on the original repository. We will be investigating a new npm namespace under which to publish future versions of this library.
|
4
|
|
-
|
5
|
|
-<br>
|
6
|
|
-
|
7
|
|
-## About Pull Requests
|
8
|
|
-
|
9
|
|
-Bugfixes should be applied to the `0.10.9` branch and new features should be applied to the `0.11.0`. Documentation/README updates can be applied directly to `master`.
|
10
|
|
-
|
11
|
|
-<br>
|
12
|
|
-
|
13
|
1
|
# react-native-fetch-blob
|
14
|
2
|
[](https://github.com/wkh237/react-native-fetch-blob/releases) [](https://www.npmjs.com/package/react-native-fetch-blob)  [](https://github.com/wkh237/react-native-fetch-blob/wiki) []()
|
15
|
3
|
|
|
@@ -30,9 +18,9 @@ A project committed to making file access and data transfer easier and more effi
|
30
|
18
|
* [Installation](#user-content-installation)
|
31
|
19
|
* [HTTP Data Transfer](#user-content-http-data-transfer)
|
32
|
20
|
* [Regular Request](#user-content-regular-request)
|
33
|
|
- * [Download file](#download-example-fetch-files-that-need-authorization-token)
|
|
21
|
+ * [Download file](#user-content-download-example--fetch-files-that-needs-authorization-token)
|
34
|
22
|
* [Upload file](#user-content-upload-example--dropbox-files-upload-api)
|
35
|
|
- * [Multipart/form upload](#multipartform-data-example-post-form-data-with-file-and-data)
|
|
23
|
+ * [Multipart/form upload](#user-content-multipartform-data-example--post-form-data-with-file-and-data)
|
36
|
24
|
* [Upload/Download progress](#user-content-uploaddownload-progress)
|
37
|
25
|
* [Cancel HTTP request](#user-content-cancel-request)
|
38
|
26
|
* [Android Media Scanner, and Download Manager Support](#user-content-android-media-scanner-and-download-manager-support)
|
|
@@ -602,10 +590,12 @@ File Access APIs
|
602
|
590
|
- [dirs](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#dirs)
|
603
|
591
|
- [createFile](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#createfilepath-data-encodingpromise)
|
604
|
592
|
- [writeFile (0.6.0)](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#writefilepathstring-contentstring--array-encodingstring-appendbooleanpromise)
|
605
|
|
-- [appendFile (0.6.0) ](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#appendfilepathstring-contentstring--array-encodingstringpromise)
|
|
593
|
+- [appendFile (0.6.0) ](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#appendfilepathstring-contentstring--arraynumber-encodingstring-promisenumber)
|
606
|
594
|
- [readFile (0.6.0)](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#readfilepath-encodingpromise)
|
607
|
|
-- [readStream](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#readstreampath-encoding-buffersizepromise)
|
608
|
|
-- [writeStream](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#writestreampathstring-encodingstring-appendbooleanpromise)
|
|
595
|
+- [readStream](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#readstreampath-encoding-buffersize-interval-promisernfbreadstream)
|
|
596
|
+- [hash (0.10.9)](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#hashpath-algorithm-promise)
|
|
597
|
+- [writeStream](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#writestreampathstring-encodingstringpromise)
|
|
598
|
+- [hash](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#hashpath-algorithmpromise)
|
609
|
599
|
- [unlink](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#unlinkpathstringpromise)
|
610
|
600
|
- [mkdir](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#mkdirpathstringpromise)
|
611
|
601
|
- [ls](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#lspathstringpromise)
|
|
@@ -648,13 +638,52 @@ RNFetchBlob.fs.readStream(
|
648
|
638
|
console.log('oops', err)
|
649
|
639
|
})
|
650
|
640
|
ifstream.onEnd(() => {
|
651
|
|
- <Image source={{ uri : 'data:image/png,base64' + data }}/>
|
|
641
|
+ <Image source={{ uri : 'data:image/png,base64' + data }}
|
652
|
642
|
})
|
653
|
643
|
})
|
654
|
644
|
```
|
655
|
645
|
|
656
|
646
|
When using `writeStream`, the stream object becomes writable, and you can then perform operations like `write` and `close`.
|
657
|
647
|
|
|
648
|
+Since version 0.10.9 `write()` resolves with the `RNFetchBlob` instance so you can promise-chain write calls:
|
|
649
|
+
|
|
650
|
+```js
|
|
651
|
+RNFetchBlob.fs.writeStream(
|
|
652
|
+ PATH_TO_FILE,
|
|
653
|
+ // encoding, should be one of `base64`, `utf8`, `ascii`
|
|
654
|
+ 'utf8',
|
|
655
|
+ // should data append to existing content ?
|
|
656
|
+ true
|
|
657
|
+)
|
|
658
|
+.then(ofstream => ofstream.write('foo'))
|
|
659
|
+.then(ofstream => ofstream.write('bar'))
|
|
660
|
+.then(ofstream => ofstream.write('foobar'))
|
|
661
|
+.then(ofstream => ofstream.close())
|
|
662
|
+.catch(console.error)
|
|
663
|
+```
|
|
664
|
+
|
|
665
|
+or
|
|
666
|
+
|
|
667
|
+```js
|
|
668
|
+RNFetchBlob.fs.writeStream(
|
|
669
|
+ PATH_TO_FILE,
|
|
670
|
+ // encoding, should be one of `base64`, `utf8`, `ascii`
|
|
671
|
+ 'utf8',
|
|
672
|
+ // should data append to existing content ?
|
|
673
|
+ true
|
|
674
|
+)
|
|
675
|
+.then(stream => Promise.all([
|
|
676
|
+ stream.write('foo'),
|
|
677
|
+ stream.write('bar'),
|
|
678
|
+ stream.write('foobar')
|
|
679
|
+]))
|
|
680
|
+// Use array destructuring to get the stream object from the first item of the array we get from Promise.all()
|
|
681
|
+.then(([stream]) => stream.close())
|
|
682
|
+.catch(console.error)
|
|
683
|
+```
|
|
684
|
+
|
|
685
|
+You should **NOT** do something like this:
|
|
686
|
+
|
658
|
687
|
```js
|
659
|
688
|
RNFetchBlob.fs.writeStream(
|
660
|
689
|
PATH_TO_FILE,
|
|
@@ -663,13 +692,18 @@ RNFetchBlob.fs.writeStream(
|
663
|
692
|
// should data append to existing content ?
|
664
|
693
|
true)
|
665
|
694
|
.then((ofstream) => {
|
|
695
|
+ // BAD IDEA - Don't do this, those writes are unchecked:
|
666
|
696
|
ofstream.write('foo')
|
667
|
697
|
ofstream.write('bar')
|
668
|
698
|
ofstream.close()
|
669
|
699
|
})
|
670
|
|
-
|
|
700
|
+.catch(console.error) // Cannot catch any write() errors!
|
671
|
701
|
```
|
672
|
702
|
|
|
703
|
+The problem with the above code is that the promises from the `ofstream.write()` calls are detached and "Lost".
|
|
704
|
+That means the entire promise chain A) resolves without waiting for the writes to finish and B) any errors caused by them are lost.
|
|
705
|
+That code may _seem_ to work if there are no errors, but those writes are of the type "fire and forget": You start them and then turn away and never know if they really succeeded.
|
|
706
|
+
|
673
|
707
|
### Cache File Management
|
674
|
708
|
|
675
|
709
|
When using `fileCache` or `path` options along with `fetch` API, response data will automatically store into the file system. The files will **NOT** removed unless you `unlink` it. There're several ways to remove the files
|