diff --git a/.eslintrc b/.eslintrc deleted file mode 100644 index 9df820fff..000000000 --- a/.eslintrc +++ /dev/null @@ -1,64 +0,0 @@ -{ - /* See all the pre-defined configs here: https://www.npmjs.com/package/eslint-config-defaults */ - "extends": "defaults/configurations/eslint", - "parser": "babel-eslint", - "plugins": [ - "react", "html" - ], - "env": { - "amd": true, - "browser": true, - "jquery": true, - "node": true, - "es6": false, - "worker": true - }, - "rules": { - - "eqeqeq": 2, - "comma-dangle": 1, - "no-console": 1, - "no-debugger": 1, - "no-extra-semi": 2, - "no-extra-parens": 1, - "no-extra-boolean-cast": 0, - "no-cond-assign": 2, - "no-irregular-whitespace": 2, - "no-undef": 0, - "no-unused-vars": 0, - "semi": 2, - "semi-spacing": 2, - "valid-jsdoc": [ - 1, - { "requireReturn": false, - "requireParamDescription": false, - "requireReturnDescription": false} - ], - - "react/display-name": 2, - "react/forbid-prop-types": 1, - "react/jsx-boolean-value": 1, - "react/jsx-closing-bracket-location": 1, - "react/jsx-curly-spacing": 1, - "react/jsx-indent-props": 0, - "react/jsx-max-props-per-line": 0, - "react/jsx-no-duplicate-props": 1, - "react/jsx-no-literals": 0, - "react/jsx-no-undef": 1, - "react/jsx-sort-props": 0, - "react/jsx-uses-react": 1, - "react/jsx-uses-vars": 1, - "react/jsx-wrap-multilines": 1, - "react/no-danger": 1, - "react/no-did-mount-set-state": 1, - "react/no-did-update-set-state": 1, - "react/no-direct-mutation-state": 1, - "react/no-multi-comp": 1, - "react/no-set-state": 0, - "react/no-unknown-property": 1, - "react/prop-types": 0, - "react/react-in-jsx-scope": 0, - "react/self-closing-comp": 1, - "react/sort-comp": 1 - } -} \ No newline at end of file diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 000000000..141baf43c --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,30 @@ +module.exports = { + root: true, + rules: { + 'prettier/prettier': 0, + "eqeqeq": 2, + "comma-dangle": 0, + "curly": 0, + "no-console": 1, + "no-debugger": 1, + "no-extra-semi": 2, + "no-extra-parens": 1, + "no-extra-boolean-cast": 1, + "no-cond-assign": 2, + "no-irregular-whitespace": 2, + "no-undef": 0, + "no-unused-vars": 0, + "semi": 2, + "semi-spacing": 2, + "valid-jsdoc": [ + 1, + { + "requireReturn": false, + "requireParamDescription": false, + "requireReturnDescription": false + } + ], + "radix": 0 + }, + "parser": "babel-eslint" +}; diff --git a/android.js b/android.js index c26860535..7df4ac2f2 100644 --- a/android.js +++ b/android.js @@ -2,9 +2,9 @@ // Use of this source code is governed by a MIT-style license that can be // found in the LICENSE file. -import { NativeModules, Platform } from 'react-native' +import { NativeModules, Platform } from 'react-native'; -const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil +const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil; /** * Send an intent to open the file. @@ -16,37 +16,37 @@ const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil function actionViewIntent(path, mime, chooserTitle) { if(typeof chooserTitle === 'undefined') chooserTitle = null; if(Platform.OS === 'android') - return ReactNativeBlobUtil.actionViewIntent(path, mime, chooserTitle) + return ReactNativeBlobUtil.actionViewIntent(path, mime, chooserTitle); else - return Promise.reject('ReactNativeBlobUtil.android.actionViewIntent only supports Android.') + return Promise.reject('ReactNativeBlobUtil.android.actionViewIntent only supports Android.'); } function getContentIntent(mime) { if(Platform.OS === 'android') - return ReactNativeBlobUtil.getContentIntent(mime) + return ReactNativeBlobUtil.getContentIntent(mime); else - return Promise.reject('ReactNativeBlobUtil.android.getContentIntent only supports Android.') + return Promise.reject('ReactNativeBlobUtil.android.getContentIntent only supports Android.'); } function addCompleteDownload(config) { if(Platform.OS === 'android') - return ReactNativeBlobUtil.addCompleteDownload(config) + return ReactNativeBlobUtil.addCompleteDownload(config); else - return Promise.reject('ReactNativeBlobUtil.android.addCompleteDownload only supports Android.') + return Promise.reject('ReactNativeBlobUtil.android.addCompleteDownload only supports Android.'); } function getSDCardDir() { if(Platform.OS === 'android') - return ReactNativeBlobUtil.getSDCardDir() + return ReactNativeBlobUtil.getSDCardDir(); else - return Promise.reject('ReactNativeBlobUtil.android.getSDCardDir only supports Android.') + return Promise.reject('ReactNativeBlobUtil.android.getSDCardDir only supports Android.'); } function getSDCardApplicationDir() { if(Platform.OS === 'android') - return ReactNativeBlobUtil.getSDCardApplicationDir() + return ReactNativeBlobUtil.getSDCardApplicationDir(); else - return Promise.reject('ReactNativeBlobUtil.android.getSDCardApplicationDir only supports Android.') + return Promise.reject('ReactNativeBlobUtil.android.getSDCardApplicationDir only supports Android.'); } @@ -56,4 +56,4 @@ export default { addCompleteDownload, getSDCardDir, getSDCardApplicationDir, -} +}; diff --git a/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java b/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java index 0c9d78ee7..9f7feb2d3 100644 --- a/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java +++ b/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java @@ -21,9 +21,7 @@ import com.facebook.react.modules.core.DeviceEventManagerModule; import java.io.*; -import java.nio.ByteBuffer; import java.nio.charset.Charset; -import java.nio.charset.CharsetEncoder; import java.security.MessageDigest; import java.util.ArrayList; import java.util.HashMap; @@ -360,20 +358,25 @@ else if (resolved == null) { fs = new FileInputStream(new File(path)); } - byte[] buffer = new byte[chunkSize]; int cursor = 0; boolean error = false; if (encoding.equalsIgnoreCase("utf8")) { - CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder(); - while ((cursor = fs.read(buffer)) != -1) { - encoder.encode(ByteBuffer.wrap(buffer).asCharBuffer()); - String chunk = new String(buffer, 0, cursor); + InputStreamReader isr = new InputStreamReader(fs, Charset.forName("UTF-8")); + BufferedReader reader = new BufferedReader(isr, chunkSize); + char[] buffer = new char[chunkSize]; + // read chunks of the string + while (reader.read(buffer, 0, chunkSize) != -1) { + String chunk = new String(buffer); emitStreamEvent(streamId, "data", chunk); if (tick > 0) SystemClock.sleep(tick); } + + reader.close(); + isr.close(); } else if (encoding.equalsIgnoreCase("ascii")) { + byte[] buffer = new byte[chunkSize]; while ((cursor = fs.read(buffer)) != -1) { WritableArray chunk = Arguments.createArray(); for (int i = 0; i < cursor; i++) { @@ -384,6 +387,7 @@ else if (resolved == null) { SystemClock.sleep(tick); } } else if (encoding.equalsIgnoreCase("base64")) { + byte[] buffer = new byte[chunkSize]; while ((cursor = fs.read(buffer)) != -1) { if (cursor < chunkSize) { byte[] copy = new byte[cursor]; @@ -407,7 +411,7 @@ else if (resolved == null) { if (!error) emitStreamEvent(streamId, "end", ""); fs.close(); - buffer = null; + } catch (FileNotFoundException err) { emitStreamEvent( streamId, @@ -710,8 +714,8 @@ static void exists(String path, Callback callback) { /** * List content of folder * - * @param path Target folder - * @param callback JS context callback + * @param path Target folder + * @param promise JS context promise */ static void ls(String path, Promise promise) { try { diff --git a/class/ReactNativeBlobUtilBlobResponse.js b/class/ReactNativeBlobUtilBlobResponse.js new file mode 100644 index 000000000..efef94314 --- /dev/null +++ b/class/ReactNativeBlobUtilBlobResponse.js @@ -0,0 +1,177 @@ +import {ReactNativeBlobUtilResponseInfo, ReactNativeBlobUtilStream} from "../types"; +import fs from "../fs"; +import Blob from "../polyfill/Blob"; +import ReactNativeBlobUtilSession from "./ReactNativeBlobUtilSession"; +import URIUtil from "../utils/uri"; + +/** + * ReactNativeBlobUtil response object class. + */ +export class FetchBlobResponse { + + taskId: string; + path: () => string | null; + type: 'base64' | 'path' | 'utf8'; + data: any; + blob: (contentType: string, sliceSize: number) => Promise; + text: () => string | Promise; + json: () => any; + base64: () => any; + flush: () => void; + respInfo: ReactNativeBlobUtilResponseInfo; + session: (name: string) => ReactNativeBlobUtilSession | null; + readFile: (encode: 'base64' | 'utf8' | 'ascii') => ?Promise; + readStream: ( + encode: 'utf8' | 'ascii' | 'base64', + ) => ReactNativeBlobUtilStream | null; + + constructor(taskId: string, info: ReactNativeBlobUtilResponseInfo, data: any) { + this.data = data; + this.taskId = taskId; + this.type = info.rnfbEncode; + this.respInfo = info; + + this.info = (): ReactNativeBlobUtilResponseInfo => { + return this.respInfo; + }; + + this.array = (): Promise => { + let cType = info.headers['Content-Type'] || info.headers['content-type']; + return new Promise((resolve, reject) => { + switch (this.type) { + case 'base64': + // TODO : base64 to array buffer + break; + case 'path': + fs.readFile(this.data, 'ascii').then(resolve); + break; + default: + // TODO : text to array buffer + break; + } + }); + }; + + /** + * Convert result to javascript ReactNativeBlobUtil object. + * @return {Promise} Return a promise resolves Blob object. + */ + this.blob = (): Promise => { + let cType = info.headers['Content-Type'] || info.headers['content-type']; + return new Promise((resolve, reject) => { + switch (this.type) { + case 'base64': + Blob.build(this.data, {type: cType + ';BASE64'}).then(resolve); + break; + case 'path': + Blob.build(URIUtil.wrap(this.data), {type: cType}).then(resolve); + break; + default: + Blob.build(this.data, {type: 'text/plain'}).then(resolve); + break; + } + }); + }; + /** + * Convert result to text. + * @return {string} Decoded base64 string. + */ + this.text = (): string | Promise => { + switch (this.type) { + case 'base64': + return base64.decode(this.data); + case 'path': + return fs.readFile(this.data, 'base64').then((b64) => Promise.resolve(base64.decode(b64))); + default: + return this.data; + } + }; + /** + * Convert result to JSON object. + * @return {object} Parsed javascript object. + */ + this.json = (): any => { + switch (this.type) { + case 'base64': + return JSON.parse(base64.decode(this.data)); + case 'path': + return fs.readFile(this.data, 'utf8') + .then((text) => Promise.resolve(JSON.parse(text))); + default: + return JSON.parse(this.data); + } + }; + /** + * Return BASE64 string directly. + * @return {string} BASE64 string of response body. + */ + this.base64 = (): string | Promise => { + switch (this.type) { + case 'base64': + return this.data; + case 'path': + return fs.readFile(this.data, 'base64'); + default: + return base64.encode(this.data); + } + }; + /** + * Remove cahced file + * @return {Promise} + */ + this.flush = () => { + let path = this.path(); + if (!path || this.type !== 'path') + return; + return fs.unlink(path); + }; + /** + * get path of response temp file + * @return {string} File path of temp file. + */ + this.path = () => { + if (this.type === 'path') + return this.data; + return null; + }; + + this.session = (name: string): ReactNativeBlobUtilSession | null => { + if (this.type === 'path') + return fs.session(name).add(this.data); + else { + console.warn('only file paths can be add into session.'); + return null; + } + }; + /** + * Start read stream from cached file + * @param {String} encoding Encode type, should be one of `base64`, `ascii`, `utf8`. + * @return {void} + */ + this.readStream = (encoding: 'base64' | 'utf8' | 'ascii'): ReactNativeBlobUtilStream | null => { + if (this.type === 'path') { + return fs.readStream(this.data, encoding); + } + else { + console.warn('ReactNativeBlobUtil', 'this response data does not contains any available stream'); + return null; + } + }; + /** + * Read file content with given encoding, if the response does not contains + * a file path, show warning message + * @param {String} encoding Encode type, should be one of `base64`, `ascrii`, `utf8`. + * @return {String} + */ + this.readFile = (encoding: 'base64' | 'utf8' | 'ascii') => { + if (this.type === 'path') { + return fs.readFile(this.data, encoding); + } + else { + console.warn('ReactNativeBlobUtil', 'this response does not contains a readable file'); + return null; + } + }; + } + +} \ No newline at end of file diff --git a/class/ReactNativeBlobUtilFile.js b/class/ReactNativeBlobUtilFile.js index a62d923ae..3a5f985ba 100644 --- a/class/ReactNativeBlobUtilFile.js +++ b/class/ReactNativeBlobUtilFile.js @@ -7,10 +7,10 @@ import { NativeModules, DeviceEventEmitter, NativeAppEventEmitter, -} from 'react-native' +} from 'react-native'; -const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil -const emitter = DeviceEventEmitter +const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil; +const emitter = DeviceEventEmitter; export default class ReactNativeBlobUtilFile { diff --git a/class/ReactNativeBlobUtilReadStream.js b/class/ReactNativeBlobUtilReadStream.js index 832ee1991..14eaac35c 100644 --- a/class/ReactNativeBlobUtilReadStream.js +++ b/class/ReactNativeBlobUtilReadStream.js @@ -6,11 +6,11 @@ import { NativeModules, DeviceEventEmitter, NativeAppEventEmitter, -} from 'react-native' -import UUID from '../utils/uuid' +} from 'react-native'; +import UUID from '../utils/uuid'; -const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil -const emitter = DeviceEventEmitter +const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil; +const emitter = DeviceEventEmitter; export default class ReactNativeBlobUtilReadStream { @@ -22,61 +22,61 @@ export default class ReactNativeBlobUtilReadStream { constructor(path:string, encoding:string, bufferSize?:?number, tick:number) { if(!path) - throw Error('ReactNativeBlobUtil could not open file stream with empty `path`') - this.encoding = encoding || 'utf8' - this.bufferSize = bufferSize - this.path = path - this.closed = false - this.tick = tick - this._onData = () => {} - this._onEnd = () => {} - this._onError = () => {} - this.streamId = 'RNFBRS'+ UUID() + throw Error('ReactNativeBlobUtil could not open file stream with empty `path`'); + this.encoding = encoding || 'utf8'; + this.bufferSize = bufferSize; + this.path = path; + this.closed = false; + this.tick = tick; + this._onData = () => {}; + this._onEnd = () => {}; + this._onError = () => {}; + this.streamId = 'RNFBRS'+ UUID(); // register for file stream event let subscription = emitter.addListener(this.streamId, (e) => { - let {event, code, detail} = e + let {event, code, detail} = e; if(this._onData && event === 'data') { - this._onData(detail) - return + this._onData(detail); + return; } else if (this._onEnd && event === 'end') { - this._onEnd(detail) + this._onEnd(detail); } else { - const err = new Error(detail) - err.code = code || 'EUNSPECIFIED' + const err = new Error(detail); + err.code = code || 'EUNSPECIFIED'; if(this._onError) - this._onError(err) + this._onError(err); else - throw err + throw err; } // when stream closed or error, remove event handler if (event === 'error' || event === 'end') { - subscription.remove() - this.closed = true + subscription.remove(); + this.closed = true; } - }) + }); } open() { if(!this.closed) - ReactNativeBlobUtil.readStream(this.path, this.encoding, this.bufferSize || 10240 , this.tick || -1, this.streamId) + ReactNativeBlobUtil.readStream(this.path, this.encoding, this.bufferSize || 10240 , this.tick || -1, this.streamId); else - throw new Error('Stream closed') + throw new Error('Stream closed'); } onData(fn:() => void) { - this._onData = fn + this._onData = fn; } onError(fn) { - this._onError = fn + this._onError = fn; } onEnd (fn) { - this._onEnd = fn + this._onEnd = fn; } } diff --git a/class/ReactNativeBlobUtilSession.js b/class/ReactNativeBlobUtilSession.js index 5b5cdc545..93ba8ce02 100644 --- a/class/ReactNativeBlobUtilSession.js +++ b/class/ReactNativeBlobUtilSession.js @@ -6,69 +6,69 @@ import { NativeModules, DeviceEventEmitter, NativeAppEventEmitter, -} from 'react-native' +} from 'react-native'; -const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil +const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil; -let sessions = {} +let sessions = {}; export default class ReactNativeBlobUtilSession { name : string; static getSession(name:string):any { - return sessions[name] + return sessions[name]; } static setSession(name:string, val:any) { - sessions[name] = val + sessions[name] = val; } static removeSession(name:string) { - delete sessions[name] + delete sessions[name]; } constructor(name:string, list:Array) { - this.name = name + this.name = name; if(!sessions[name]) { if(Array.isArray(list)) - sessions[name] = list + sessions[name] = list; else - sessions[name] = [] + sessions[name] = []; } } add(path:string):ReactNativeBlobUtilSession { - sessions[this.name].push(path) - return this + sessions[this.name].push(path); + return this; } remove(path:string):ReactNativeBlobUtilSession { - let list = sessions[this.name] + let list = sessions[this.name]; for(let i of list) { if(list[i] === path) { - sessions[this.name].splice(i, 1) + sessions[this.name].splice(i, 1); break; } } - return this + return this; } list():Array { - return sessions[this.name] + return sessions[this.name]; } dispose():Promise { return new Promise((resolve, reject) => { ReactNativeBlobUtil.removeSession(sessions[this.name], (err) => { if(err) - reject(new Error(err)) + reject(new Error(err)); else { - delete sessions[this.name] - resolve() + delete sessions[this.name]; + resolve(); } - }) - }) + }); + }); } } diff --git a/class/ReactNativeBlobUtilWriteStream.js b/class/ReactNativeBlobUtilWriteStream.js index 4f1863909..ea9f853b2 100644 --- a/class/ReactNativeBlobUtilWriteStream.js +++ b/class/ReactNativeBlobUtilWriteStream.js @@ -6,9 +6,9 @@ import { NativeModules, DeviceEventEmitter, NativeAppEventEmitter, -} from 'react-native' +} from 'react-native'; -const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil +const ReactNativeBlobUtil = NativeModules.ReactNativeBlobUtil; export default class ReactNativeBlobUtilWriteStream { @@ -17,41 +17,41 @@ export default class ReactNativeBlobUtilWriteStream { append : boolean; constructor(streamId:string, encoding:string, append:boolean) { - this.id = streamId - this.encoding = encoding - this.append = append + this.id = streamId; + this.encoding = encoding; + this.append = append; } write(data:string): Promise { return new Promise((resolve, reject) => { try { - let method = this.encoding === 'ascii' ? 'writeArrayChunk' : 'writeChunk' + let method = this.encoding === 'ascii' ? 'writeArrayChunk' : 'writeChunk'; if(this.encoding.toLocaleLowerCase() === 'ascii' && !Array.isArray(data)) { - reject(new Error('ascii input data must be an Array')) - return + reject(new Error('ascii input data must be an Array')); + return; } ReactNativeBlobUtil[method](this.id, data, (error) => { if(error) - reject(new Error(error)) + reject(new Error(error)); else - resolve(this) - }) + resolve(this); + }); } catch(err) { - reject(new Error(err)) + reject(new Error(err)); } - }) + }); } close() { return new Promise((resolve, reject) => { try { ReactNativeBlobUtil.closeStream(this.id, () => { - resolve() - }) + resolve(); + }); } catch (err) { - reject(new Error(err)) + reject(new Error(err)); } - }) + }); } } diff --git a/examples/ReactNativeBlobUtil/App.js b/examples/ReactNativeBlobUtil/App.js index d20484545..9754286fd 100644 --- a/examples/ReactNativeBlobUtil/App.js +++ b/examples/ReactNativeBlobUtil/App.js @@ -6,952 +6,693 @@ * @flow strict-local */ -import React, { useState } from 'react'; -import { - SafeAreaView, - StyleSheet, - ScrollView, - View, - Text, - StatusBar, - TextInput, - Button, - Alert, - Picker, -} from 'react-native'; - -import { - Colors, -} from 'react-native/Libraries/NewAppScreen'; +import React, {useState} from 'react'; +import {SafeAreaView, StyleSheet, ScrollView, View, Text, StatusBar, TextInput, Button, Alert, Picker} from 'react-native'; + +import {Colors} from 'react-native/Libraries/NewAppScreen'; import ReactNativeBlobUtil from 'react-native-blob-util'; const App: () => React$Node = () => { + // Variables ****************************************************************** + const [existsParam, setExistsParam] = useState(''); + const [lsParam, setLSParam] = useState(''); + + const [cpSourceParam, setCPSourceParam] = useState(''); + const [cpDestParam, setCPDestParam] = useState(''); + + const [unlinkParam, setUnlinkParam] = useState(''); + + const [statParam, setStatParam] = useState(''); + + const [mkdirParam, setMkdirParam] = useState(''); + const [mkdirURIParam, setMkdirURIParam] = useState(''); + + const [readParam, setReadParam] = useState(''); + + const [hashPathParam, setHashPathParam] = useState(''); + const [hashAlgValue, setHashAlgValue] = useState('md5'); + + const [writeParam, setWriteParam] = useState(''); + const [writeURIParam, setWriteURIParam] = useState(''); + const [writeEncodeParam, setWriteEncodeParam] = useState('utf8'); + + const [writeStreamParam, setWriteStreamParam] = useState(''); + const [writeEncodeStreamParam, setWriteStreamEncodeParam] = useState('utf8'); + + const [readStreamParam, setReadStreamParam] = useState(''); + const [readEncodeStreamParam, setReadStreamEncodeParam] = useState('utf8'); + + // Methods ******************************************************************** + // exists() + const existsCall = () => { + ReactNativeBlobUtil.fs + .exists(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + existsParam) + .then((result) => { + Alert.alert('Exists: ' + result); + }) + .catch((err) => { + Alert.alert(err.message); + }); + }; + + const isDirCall = () => { + ReactNativeBlobUtil.fs + .exists(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + existsParam) + .then((result) => { + Alert.alert('isDir: ' + result); + }) + .catch((err) => { + Alert.alert(err.message); + }); + }; + + // df() + const dfCall = () => { + ReactNativeBlobUtil.fs + .df() + .then((result) => { + Alert.alert('Free space: ' + result.free + ' bytes\nTotal space: ' + result.total + ' bytes'); + }) + .catch((err) => { + Alert.alert(err.message); + }); + }; + + // ls() + const lsCall = () => { + ReactNativeBlobUtil.fs.ls(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + lsParam).then((files) => { + Alert.alert('Method finished: check debug console for results'); + console.log(files); + }); + }; + + // cp() + const cpCall = () => { + ReactNativeBlobUtil.fs + .cp(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + cpSourceParam, ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + cpDestParam) + .then(Alert.alert('File successfully copied')) + .catch((err) => { + Alert.alert(err.message); + }); + }; + + // mv() + const mvCall = () => { + ReactNativeBlobUtil.fs + .mv(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + cpSourceParam, ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + cpDestParam) + .then(Alert.alert('File successfully moved')) + .catch((err) => { + Alert.alert(err.message); + }); + }; + + // unlink() + const unlinkCall = () => { + ReactNativeBlobUtil.fs + .unlink(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + unlinkParam) + .then(Alert.alert('file/directory successfully unlinked')) + .catch((err) => { + Alert.alert(err.message); + }); + }; + + // stat(), lstat() + const statCall = () => { + ReactNativeBlobUtil.fs + .stat(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + statParam) + .then((stats) => { + console.log(stats); + Alert.alert('stat() result (others logged in console)', 'filename: ' + stats.filename + '\nlastModified: ' + stats.lastModified + '\npath: ' + stats.path + '\nsize: ' + stats.size + '\ntype: ' + stats.type); + }) + .catch((err) => { + Alert.alert(err.message); + }); + }; + + const lstatCall = () => { + ReactNativeBlobUtil.fs + .lstat(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + statParam) + .then((stats) => { + console.log(stats); + Alert.alert('lstat() result (others logged in console)', 'filename: ' + stats[0].filename + '\nlastModified: ' + stats[0].lastModified + '\npath: ' + stats[0].path + '\nsize: ' + stats[0].size + '\ntype: ' + stats[0].type); + }) + .catch((err) => { + Alert.alert(err.message); + }); + }; + + // mkdir() + const mkdirCall = () => { + if (mkdirParam.length > 0) { + ReactNativeBlobUtil.fs + .mkdir(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + mkdirParam) + .then(() => { + Alert.alert('successfully created file:', ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + mkdirParam); + }) + .catch((err) => { + Alert.alert(err.message); + }); + } else { + Alert.alert('Cannot make file with no name provided'); + } + }; + + // createFile() + const createFileUTF8Call = () => { + ReactNativeBlobUtil.fs.createFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + mkdirParam, 'foo', 'utf8'); + }; + + const createFileASCIICall = () => { + ReactNativeBlobUtil.fs.createFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + mkdirParam, [102, 111, 111], 'ascii'); + }; + + const createFileBase64Call = () => { + ReactNativeBlobUtil.fs.createFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + mkdirParam, 'Zm9v', 'base64'); + }; + + const createFileURICall = () => { + ReactNativeBlobUtil.fs.createFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + mkdirParam, ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + mkdirURIParam, 'uri'); + }; + + // readFile() + const readFileUTF8Call = () => { + ReactNativeBlobUtil.fs.readFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + readParam, 'utf8').then((data) => { + Alert.alert('UTF8 result of ' + readParam, data); + }); + }; -// Variables ****************************************************************** - const [existsParam, setExistsParam] = useState(''); - const [lsParam, setLSParam] = useState(''); - - const [cpSourceParam, setCPSourceParam] = useState(''); - const [cpDestParam, setCPDestParam] = useState(''); - - const [unlinkParam, setUnlinkParam] = useState(''); - - const [statParam, setStatParam] = useState(''); - - const [mkdirParam, setMkdirParam] = useState(''); - const [mkdirURIParam, setMkdirURIParam] = useState(''); - - const [readParam, setReadParam] = useState(''); - - const [hashPathParam, setHashPathParam] = useState(''); - const [hashAlgValue, setHashAlgValue] = useState('md5'); - - const [writeParam, setWriteParam] = useState(''); - const [writeURIParam, setWriteURIParam] = useState(''); - const [writeEncodeParam, setWriteEncodeParam] = useState('utf8'); - - const [writeStreamParam, setWriteStreamParam] = useState(''); - const [writeEncodeStreamParam, setWriteStreamEncodeParam] = useState('utf8'); - - const [readStreamParam, setReadStreamParam] = useState(''); - const [readEncodeStreamParam, setReadStreamEncodeParam] = useState('utf8'); - -// Methods ******************************************************************** - // exists() - const existsCall = () => { - ReactNativeBlobUtil.fs.exists(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + existsParam) - .then((result) => { - Alert.alert('Exists: ' + result) - }) - .catch((err) => { - Alert.alert(err.message); - }) - } - - const isDirCall = () => { - ReactNativeBlobUtil.fs.exists(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + existsParam) - .then((result) => { - Alert.alert('isDir: ' + result); - }) - .catch((err) => { - Alert.alert(err.message); - }) - } - - // df() - const dfCall = () => { - ReactNativeBlobUtil.fs.df() - .then((result) => { - Alert.alert('Free space: ' + result.free + ' bytes\nTotal space: ' + result.total + ' bytes'); - }) - .catch((err) => { - Alert.alert(err.message); - }) - } - - // ls() - const lsCall = () => { - ReactNativeBlobUtil.fs.ls(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + lsParam) - .then((files) => { - Alert.alert('Method finished: check debug console for results'); - console.log(files); - }) - } - - // cp() - const cpCall = () => { - ReactNativeBlobUtil.fs.cp(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + cpSourceParam, - ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + cpDestParam) - .then( - Alert.alert('File successfully copied') - ) - .catch((err) => { - Alert.alert(err.message); - }); - } - - // mv() - const mvCall = () => { - ReactNativeBlobUtil.fs.mv(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + cpSourceParam, - ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + cpDestParam) - .then( - Alert.alert('File successfully moved') - ) - .catch((err) => { - Alert.alert(err.message); - }); - } - - // unlink() - const unlinkCall = () => { - ReactNativeBlobUtil.fs.unlink(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + unlinkParam) - .then( - Alert.alert('file/directory successfully unlinked') - ) - .catch((err) => { - Alert.alert(err.message); - }) - } - - // stat(), lstat() - const statCall = () => { - ReactNativeBlobUtil.fs.stat(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + statParam) - .then((stats) => { - console.log(stats); - Alert.alert("stat() result (others logged in console)", - "filename: " + stats.filename + - "\nlastModified: " + stats.lastModified + - "\npath: " + stats.path + - "\nsize: " + stats.size + - "\ntype: " + stats.type) - }) - .catch((err) => { - Alert.alert(err.message); - }) - } - - const lstatCall = () => { - ReactNativeBlobUtil.fs.lstat(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + statParam) - .then((stats) => { - console.log(stats); - Alert.alert("lstat() result (others logged in console)", "filename: " + stats[0].filename + - "\nlastModified: " + stats[0].lastModified + - "\npath: " + stats[0].path + - "\nsize: " + stats[0].size + - "\ntype: " + stats[0].type); - }) - .catch((err) => { - Alert.alert(err.message); - }) - } - - // mkdir() - const mkdirCall = () => { - if(mkdirParam.length > 0) - { - ReactNativeBlobUtil.fs.mkdir(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + mkdirParam) - .then(() => { - Alert.alert('successfully created file:', ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + mkdirParam); - }) - .catch((err) => { - Alert.alert(err.message); - }) - } - else { - Alert.alert('Cannot make file with no name provided') - } - } - - // createFile() - const createFileUTF8Call = () => { - ReactNativeBlobUtil.fs.createFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + mkdirParam, 'foo', 'utf8'); - } - - const createFileASCIICall = () => { - ReactNativeBlobUtil.fs.createFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + mkdirParam, [102, 111, 111], 'ascii'); - } - - const createFileBase64Call = () => { - ReactNativeBlobUtil.fs.createFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + mkdirParam, 'Zm9v', 'base64'); - } - - const createFileURICall = () => { - ReactNativeBlobUtil.fs.createFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + mkdirParam, ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + mkdirURIParam, 'uri'); - } - - // readFile() - const readFileUTF8Call = () => { - ReactNativeBlobUtil.fs.readFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + readParam, 'utf8') - .then((data) => { - Alert.alert('UTF8 result of ' + readParam, data); - }); - } - - const readFileASCIICall = () => { - ReactNativeBlobUtil.fs.readFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + readParam, 'ascii') - .then((data) => { - Alert.alert('UTF8 result of ' + readParam, data); - }); - } - - const readFileBase64Call = () => { - ReactNativeBlobUtil.fs.readFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + readParam, 'base64') - .then((data) => { - Alert.alert('UTF8 result of ' + readParam, data); - }) - } - - // hash() - const hashCall = () => { - ReactNativeBlobUtil.fs.hash(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + hashPathParam, hashAlgValue) - .then((hash) => { - Alert.alert(hashAlgValue, hash); - }) - .catch((err) => { - console.log(hashAlgValue + ': ' + err); - }); - } - - - // writeFile() - const writeFileCall = () => { - if(writeParam.length > 0) { - if(writeEncodeParam === 'uri') { - if(writeURIParam.length > 0) { - ReactNativeBlobUtil.fs.writeFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeParam, - ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeURIParam, - writeEncodeParam); + const readFileASCIICall = () => { + ReactNativeBlobUtil.fs.readFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + readParam, 'ascii').then((data) => { + Alert.alert('UTF8 result of ' + readParam, data); + }); + }; + + const readFileBase64Call = () => { + ReactNativeBlobUtil.fs.readFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + readParam, 'base64').then((data) => { + Alert.alert('UTF8 result of ' + readParam, data); + }); + }; + + // hash() + const hashCall = () => { + ReactNativeBlobUtil.fs + .hash(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + hashPathParam, hashAlgValue) + .then((hash) => { + Alert.alert(hashAlgValue, hash); + }) + .catch((err) => { + console.log(hashAlgValue + ': ' + err); + }); + }; + + // writeFile() + const writeFileCall = () => { + if (writeParam.length > 0) { + if (writeEncodeParam === 'uri') { + if (writeURIParam.length > 0) { + ReactNativeBlobUtil.fs.writeFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeParam, ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeURIParam, writeEncodeParam); + } else { + Alert.alert('uri path undefined'); + } + } else if (writeEncodeParam === 'ascii') { + ReactNativeBlobUtil.fs.writeFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeParam, [102, 111, 111], writeEncodeParam); + } else { + ReactNativeBlobUtil.fs.writeFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeParam, 'foo', writeEncodeParam); + } } - else { - Alert.alert('uri path undefined'); + }; + + // appendFile() + const appendFileCall = () => { + if (writeParam.length > 0) { + if (writeEncodeParam === 'uri') { + if (writeURIParam.length > 0) { + ReactNativeBlobUtil.fs.appendFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeParam, ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeURIParam, writeEncodeParam); + } else { + Alert.alert('uri path undefined'); + } + } else if (writeEncodeParam === 'ascii') { + ReactNativeBlobUtil.fs.appendFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeParam, [102, 111, 111], writeEncodeParam); + } else { + ReactNativeBlobUtil.fs.appendFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeParam, 'foo', writeEncodeParam); + } } - } - else if(writeEncodeParam === 'ascii') { - ReactNativeBlobUtil.fs.writeFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeParam, [102,111,111], writeEncodeParam); - } - else { - ReactNativeBlobUtil.fs.writeFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeParam, 'foo', writeEncodeParam); - } - } - } - - // appendFile() - const appendFileCall = () => { - if(writeParam.length > 0) { - if(writeEncodeParam === 'uri') { - if(writeURIParam.length > 0) { - ReactNativeBlobUtil.fs.appendFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeParam, - ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeURIParam, - writeEncodeParam); + }; + + const writeStreamCall = () => { + if (writeStreamParam.length > 0) { + if (writeEncodeStreamParam === 'base64') { + ReactNativeBlobUtil.fs.writeStream(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, false).then((stream) => { + stream.write('Zm9vIChXcml0ZSBCYXNlNjQpMQ=='); + stream.write('Zm9vIChXcml0ZSBCYXNlNjQpMg=='); + return stream.close(); + }); + } else if (writeEncodeStreamParam === 'ascii') { + ReactNativeBlobUtil.fs.writeStream(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, false).then((stream) => { + stream.write([102, 111, 111, 32, 40, 87, 114, 105, 116]); + stream.write([101, 32, 97, 115, 99, 105, 105, 41]); + return stream.close(); + }); + } else { + ReactNativeBlobUtil.fs.writeStream(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, false).then((stream) => { + stream.write('foo (Write utf8)1'); + stream.write('foo (Write utf8)2'); + return stream.close(); + }); + } } - else { - Alert.alert('uri path undefined'); + }; + + const appendStreamCall = () => { + if (writeStreamParam.length > 0) { + if (writeEncodeStreamParam === 'base64') { + ReactNativeBlobUtil.fs.writeStream(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, true).then((stream) => { + stream.write('Zm9vIChBcHBlbmQgQmFzZTY0KTE='); + stream.write('Zm9vIChBcHBlbmQgQmFzZTY0KTI='); + return stream.close(); + }); + } else if (writeEncodeStreamParam === 'ascii') { + ReactNativeBlobUtil.fs.writeStream(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, true).then((stream) => { + stream.write([102, 111, 111, 32, 40]); + stream.write([65, 112, 112, 101, 110, 100, 32, 65, 83, 67, 73, 73, 41]); + return stream.close(); + }); + } else { + ReactNativeBlobUtil.fs.writeStream(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, true).then((stream) => { + stream.write('foo (Append utf8)1'); + stream.write('foo (Append utf8)2'); + return stream.close(); + }); + } } - } - else if(writeEncodeParam === 'ascii') { - ReactNativeBlobUtil.fs.appendFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeParam, [102,111,111], writeEncodeParam); - } - else { - ReactNativeBlobUtil.fs.appendFile(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeParam, 'foo', writeEncodeParam); - } - } - } - - const writeStreamCall = () => { - if(writeStreamParam.length > 0) { - if(writeEncodeStreamParam === 'base64') { - ReactNativeBlobUtil.fs.writeStream(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, false) - .then((stream) => { - stream.write('Zm9vIChXcml0ZSBCYXNlNjQpMQ=='); - stream.write('Zm9vIChXcml0ZSBCYXNlNjQpMg=='); - return stream.close(); - }); - } - else if(writeEncodeStreamParam=== 'ascii') { - ReactNativeBlobUtil.fs.writeStream(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, false) - .then((stream) => { - stream.write([102, 111, 111, 32, 40, 87, 114, 105, 116]); - stream.write([ 101, 32, 97, 115, 99, 105, 105, 41]); - return stream.close(); - }); - } - else { - ReactNativeBlobUtil.fs.writeStream(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, false) - .then((stream) => { - stream.write('foo (Write utf8)1'); - stream.write('foo (Write utf8)2'); - return stream.close(); - }); - } - } - } - - const appendStreamCall = () => { - if(writeStreamParam.length > 0) { - if(writeEncodeStreamParam === 'base64') { - ReactNativeBlobUtil.fs.writeStream(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, true) - .then((stream) => { - stream.write('Zm9vIChBcHBlbmQgQmFzZTY0KTE='); - stream.write('Zm9vIChBcHBlbmQgQmFzZTY0KTI='); - return stream.close(); - }); - } - else if(writeEncodeStreamParam=== 'ascii') { - ReactNativeBlobUtil.fs.writeStream(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, true) - .then((stream) => { - stream.write([102, 111, 111, 32, 40]); - stream.write([65, 112, 112, 101, 110, 100, 32, 65, 83, 67, 73, 73, 41]); - return stream.close(); - }); - } - else { - ReactNativeBlobUtil.fs.writeStream(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, true) - .then((stream) => { - stream.write('foo (Append utf8)1'); - stream.write('foo (Append utf8)2'); - return stream.close(); + }; + + // readStream + const readStreamCall = () => { + ReactNativeBlobUtil.fs.readStream(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + readStreamParam, readEncodeStreamParam, 4000, 200).then((stream) => { + let data = ''; + stream.open(); + stream.onData((chunk) => { + data += chunk; + }); + stream.onEnd(() => { + console.log('data: ' + data); + }); }); - } - } - } - - // readStream - const readStreamCall = () => { - ReactNativeBlobUtil.fs.readStream(ReactNativeBlobUtil.fs.dirs.DocumentDir + '/' + readStreamParam, readEncodeStreamParam, 4000, 200) - .then((stream) => { - let data = ''; - stream.open(); - stream.onData((chunk) => { - data += chunk; - }) - stream.onEnd(() => { - console.log('data: ' + data); - }) - }); - } - - // fetchCall - const fetchCall = () => { - ReactNativeBlobUtil.config({ - // add this option that makes response data to be stored as a file, - // this is much more performant. - fileCache : true, - }) - .fetch( - 'GET', - 'https://upload.wikimedia.org/wikipedia/commons/c/c4/Change-5.png', - ) - .then((res) => { - // the temp file path - console.log('The file saved to ', res.path()) - }); - } + }; + + // fetchCall + const fetchCall = () => { + ReactNativeBlobUtil.config({ + // add this option that makes response data to be stored as a file, + // this is much more performant. + fileCache: true, + }) + .fetch('GET', 'https://upload.wikimedia.org/wikipedia/commons/c/c4/Change-5.png') + .then((res) => { + // the temp file path + console.log('The file saved to ', res.path()); + }); + }; // uploadFileFromStorage const uploadFromStorageCall = () => { - ReactNativeBlobUtil.fetch('POST', 'https://enb954aqyumba.x.pipedream.net/', { - Authorization : "Bearer access-token...", - 'Dropbox-API-Arg': JSON.stringify({ - path : '/img-from-react-native.png', - mode : 'add', - autorename : true, - mute : false - }), - 'Content-Type' : 'application/octet-stream', - // here's the body you're going to send, should be a BASE64 encoded string - // (you can use "base64"(refer to the library 'mathiasbynens/base64') APIs to make one). - // The data will be converted to "byte array"(say, blob) before request sent. - }, ReactNativeBlobUtil.wrap(ReactNativeBlobUtil.fs.dirs.DocumentDir + '\\ImageToUpload.jpg')) - .then((res) => { - console.log(res.text()); - }) - .catch((err) => { - // error handling .. - }) - } - - // uploadTextFromStorage - const uploadTextFromCall = () => { - ReactNativeBlobUtil.fetch('POST', 'https://enb954aqyumba.x.pipedream.net/', { - Authorization : "Bearer access-token...", - 'Dropbox-API-Arg': JSON.stringify({ - path : '/img-from-react-native.png', - mode : 'add', - autorename : true, - mute : false - }), - 'Content-Type' : 'application/octet-stream', - // here's the body you're going to send, should be a BASE64 encoded string - // (you can use "base64"(refer to the library 'mathiasbynens/base64') APIs to make one). - // The data will be converted to "byte array"(say, blob) before request sent. - }, "Waka Flacka Flame goes very well with Thomas the Tank Engine.") - .then((res) => { - console.log(res.text()); - }) - .catch((err) => { - // error handling .. - }) - } - - // MultipartFileAndData - const MultipartFileAndData = () => { - ReactNativeBlobUtil.fetch('POST', 'https://enb954aqyumba.x.pipedream.net/', { - Authorization : "Bearer access-token...", - 'Dropbox-API-Arg': JSON.stringify({ - path : '/img-from-react-native.png', - mode : 'add', - autorename : true, - mute : false - }), - 'Content-Type' : 'application/octet-stream', - // here's the body you're going to send, should be a BASE64 encoded string - // (you can use "base64"(refer to the library 'mathiasbynens/base64') APIs to make one). - // The data will be converted to "byte array"(say, blob) before request sent. - }, "Waka Flacka Flame goes very well with Thomas the Tank Engine.") - .uploadProgress((received, total) => { - console.log('upload progress', received / total) - }) - .progress((received, total) => { - console.log('download progress', received / total) - }) - .then((res) => { - console.log(res.text()); - }) - .catch((err) => { - // error handling .. - }) - } - - // - const MakeRequestWithProgress = () => { - ReactNativeBlobUtil.config({ - // add this option that makes response data to be stored as a file, - // this is much more performant. - fileCache : true, - }).fetch('POST', 'https://enb954aqyumba.x.pipedream.net/', { - Authorization : "Bearer access-token", - otherHeader : "foo", - 'Content-Type' : 'multipart/form-data', - }, [ - // element with property `filename` will be transformed into `file` in form data - { name : 'avatar', filename : 'avatar.png', data: "Kentucky Fried Seth"}, - // custom content type - { name : 'avatar-png', filename : 'avatar-png.png', type:'image/png', data: "whaddup my pickles"}, - // part file from storage - { name : 'avatar-foo', filename : 'avatar-foo.png', type:'image/foo', data: ReactNativeBlobUtil.wrap(ReactNativeBlobUtil.fs.dirs.DocumentDir + '\\ImageToUpload.jpg')}, - // elements without property `filename` will be sent as plain text - { name : 'name', data : 'user'}, - { name : 'info', data : JSON.stringify({ - mail : 'example@example.com', - tel : '12345678' - })}, - ]).uploadProgress({ interval : 250 }, (written, total) => { - console.log('uploaded', written / total) - }).progress({ count : 10, interval: -1 }, (received, total) => { - console.log('progress', received / total) - }).then((res) => { - console.log(res.text()); - }).catch((err) => { - console.log(err.text()); - }) - } - -// App ************************************************************************ - return ( - <> - - - - {global.HermesInternal == null ? null : ( - - Engine: Hermes - - )} - - {"React Native Fetch Blob Windows Demo App"} - - - - - - {"DocumentDir: " + ReactNativeBlobUtil.fs.dirs.DocumentDir + '\n'} - {"CacheDir: " + ReactNativeBlobUtil.fs.dirs.CacheDir + '\n'} - {"PictureDir: " + ReactNativeBlobUtil.fs.dirs.PictureDir + '\n'} - {"MusicDir: " + ReactNativeBlobUtil.fs.dirs.MusicDir + '\n'} - {"DownloadDir: " + ReactNativeBlobUtil.fs.dirs.DownloadDir + '\n'} - {"DCIMDir: " + ReactNativeBlobUtil.fs.dirs.DCIMDir + '\n'} - {"SDCardDir: " + ReactNativeBlobUtil.fs.dirs.SDCardDir + '\n'} - {"SDCardApplicationDir: " + ReactNativeBlobUtil.fs.dirs.SDCardApplicationDir + '\n'} - {"MainBundleDir: " + ReactNativeBlobUtil.fs.dirs.MainBundleDir + '\n'} - {"LibraryDir: " + ReactNativeBlobUtil.fs.dirs.LibraryDir + '\n'} - - - - - - - - - {"exists - exists(), isDir()"} - - setExistsParam(existsParam)} - placeholderTextColor = "#9a73ef" - autoCapitalize = "none" - /> -