You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using `writeStream`, the stream object becomes writable, and you can then perform operations like `write` and `close`.
645
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
+
646
687
```js
647
688
RNFetchBlob.fs.writeStream(
648
689
PATH_TO_FILE,
@@ -651,13 +692,18 @@ RNFetchBlob.fs.writeStream(
651
692
// should data append to existing content ?
652
693
true)
653
694
.then((ofstream) => {
695
+
// BAD IDEA - Don't do this, those writes are unchecked:
654
696
ofstream.write('foo')
655
697
ofstream.write('bar')
656
698
ofstream.close()
657
699
})
658
-
700
+
.catch(console.error) // Cannot catch any write() errors!
659
701
```
660
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
+
661
707
### Cache File Management
662
708
663
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
promise.reject("RNFetchblob.addCompleteDownload can not resolve URI:" + config.getString("path"), "RNFetchblob.addCompleteDownload can not resolve URI:" + path);
364
+
promise.reject("EINVAL", "RNFetchblob.addCompleteDownload can not resolve URI:" + config.getString("path"));
RNFetchBlobUtils.emitWarningEvent("RNFetchBlob multipart request builder has found a field without `data` property, the field `"+ field.name +"` will be removed implicitly.");
306
301
}
307
302
elseif (field.filename != null) {
303
+
Stringdata = field.data;
308
304
// upload from storage
309
305
if (data.startsWith(RNFetchBlobConst.FILE_PREFIX)) {
0 commit comments