GithubHelp home page GithubHelp logo

wkh237 / react-native-fetch-blob Goto Github PK

View Code? Open in Web Editor NEW
2.6K 50.0 1.6K 51.26 MB

A project committed to making file access and data transfer easier, efficient for React Native developers.

License: MIT License

Java 30.72% JavaScript 43.42% Objective-C 25.70% Ruby 0.15%
file-access react-native storage network file-system file polyfill android ios

react-native-fetch-blob's Introduction

New Maintainers and Repository Location

This repository no longer is the main location of "react-native-fetch-blob".

The owners of this fork have agreed to maintain this package:

https://github.com/joltup/rn-fetch-blob

That means issues and PRs should be posted there.


react-native-fetch-blob

release npm npm

A project committed to making file access and data transfer easier and more efficient for React Native developers.

For Firebase Storage solution, please upgrade to the latest version for the best compatibility.

Features

  • Transfer data directly from/to storage without BASE64 bridging
  • File API supports regular files, Asset files, and CameraRoll files
  • Native-to-native file manipulation API, reduce JS bridging performance loss
  • File stream support for dealing with large file
  • Blob, File, XMLHttpRequest polyfills that make browser-based library available in RN (experimental)
  • JSON stream supported base on Oboe.js @jimhigson

TOC (visit Wiki to get the complete documentation)

About

This project was started in the cause of solving issue facebook/react-native#854, React Native's lacks of Blob implementation which results into problems when transferring binary data.

It is committed to making file access and transfer easier and more efficient for React Native developers. We've implemented highly customizable filesystem and network module which plays well together. For example, developers can upload and download data directly from/to storage, which is more efficient, especially for large files. The file system supports file stream, so you don't have to worry about OOM problem when accessing large files.

In 0.8.0 we introduced experimental Web API polyfills that make it possible to use browser-based libraries in React Native, such as, FireBase JS SDK

Installation

Install package from npm

npm install --save react-native-fetch-blob

Or if using CocoaPods, add the pod to your Podfile

pod 'react-native-fetch-blob',
    :path => '../node_modules/react-native-fetch-blob'

After 0.10.3 you can install this package directly from Github

# replace <branch_name> with any one of the branches
npm install --save github:wkh237/react-native-fetch-blob-package#<branch_name>

Automatically Link Native Modules

For 0.29.2+ projects, simply link native packages via the following command (note: rnpm has been merged into react-native)

react-native link

As for projects < 0.29 you need rnpm to link native packages

rnpm link

Optionally, use the following command to add Android permissions to AndroidManifest.xml automatically

RNFB_ANDROID_PERMISSIONS=true react-native link

pre 0.29 projects

RNFB_ANDROID_PERMISSIONS=true rnpm link

The link script might not take effect if you have non-default project structure, please visit the wiki to link the package manually.

Grant Permission to External storage for Android 5.0 or lower

The mechanism for granting Android permissions has slightly different since Android 6.0 released, please refer to Official Document.

If you're going to access external storage (say, SD card storage) for Android 5.0 (or lower) devices, you might have to add the following line to AndroidManifest.xml.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rnfetchblobtest"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
+   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />                                               
+   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />                                              

    ...

Also, if you're going to use Android Download Manager you have to add this to AndroidManifest.xml

    <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
+           <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>                          
    </intent-filter>

Grant Access Permission for Android 6.0

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. So adding permissions in AndroidManifest.xml won't work for Android 6.0+ devices. To grant permissions in runtime, you might use PermissionAndroid API.

Usage

ES6

The module uses ES6 style export statement, simply use import to load the module.

import RNFetchBlob from 'react-native-fetch-blob'

ES5

If you're using ES5 require statement to load the module, please add default. See here for more detail.

var RNFetchBlob = require('react-native-fetch-blob').default

HTTP Data Transfer

Regular Request

After 0.8.0 react-native-fetch-blob automatically decides how to send the body by checking its type and Content-Type in the header. The rule is described in the following diagram

To sum up:

  • To send a form data, the Content-Type header does not matter. When the body is an Array we will set proper content type for you.
  • To send binary data, you have two choices, use BASE64 encoded string or path points to a file contains the body.
  • If the Content-Type containing substring;BASE64 or application/octet the given body will be considered as a BASE64 encoded data which will be decoded to binary data as the request body.
  • Otherwise, if a string starts with RNFetchBlob-file:// (which can simply be done by RNFetchBlob.wrap(PATH_TO_THE_FILE)), it will try to find the data from the URI string after RNFetchBlob-file:// and use it as the request body.
  • To send the body as-is, simply use a Content-Type header not containing ;BASE64 or application/octet.

It is Worth to mentioning that the HTTP request uses cache by default, if you're going to disable it simply add a Cache-Control header 'Cache-Control' : 'no-store'

After 0.9.4, we disabled Chunked transfer encoding by default, if you're going to use it, you should explicitly set header Transfer-Encoding to Chunked.

Download example: Fetch files that need authorization token

Most simple way is download to memory and stored as BASE64 encoded string, this is handy when the response data is small.

// send http request in a new thread (using native code)
RNFetchBlob.fetch('GET', 'http://www.example.com/images/img1.png', {
    Authorization : 'Bearer access-token...',
    // more headers  ..
  })
  // when response status code is 200
  .then((res) => {
    // the conversion is done in native code
    let base64Str = res.base64()
    // the following conversions are done in js, it's SYNC
    let text = res.text()
    let json = res.json()

  })
  // Status code is not 200
  .catch((errorMessage, statusCode) => {
    // error handling
  })

Download to storage directly

If the response data is large, that would be a bad idea to convert it into BASE64 string. A better solution is streaming the response directly into a file, simply add a fileCache option to config, and set it to true. This will make incoming response data stored in a temporary path without any file extension.

These files won't be removed automatically, please refer to Cache File Management

RNFetchBlob
  .config({
    // add this option that makes response data to be stored as a file,
    // this is much more performant.
    fileCache : true,
  })
  .fetch('GET', 'http://www.example.com/file/example.zip', {
    //some headers ..
  })
  .then((res) => {
    // the temp file path
    console.log('The file saved to ', res.path())
  })

Set Temp File Extension

Sometimes you might need a file extension for some reason. For example, when using file path as the source of Image component, the path should end with something like .png or .jpg, you can do this by add appendExt option to config.

RNFetchBlob
  .config({
    fileCache : true,
    // by adding this option, the temp files will have a file extension
    appendExt : 'png'
  })
  .fetch('GET', 'http://www.example.com/file/example.zip', {
    //some headers ..
  })
  .then((res) => {
    // the temp file path with file extension `png`
    console.log('The file saved to ', res.path())
    // Beware that when using a file path as Image source on Android,
    // you must prepend "file://"" before the file path
    imageView = <Image source={{ uri : Platform.OS === 'android' ? 'file://' + res.path()  : '' + res.path() }}/>
  })

Use Specific File Path

If you prefer a particular file path rather than randomly generated one, you can use path option. We've added several constants in v0.5.0 which represents commonly used directories.

let dirs = RNFetchBlob.fs.dirs
RNFetchBlob
.config({
  // response data will be saved to this path if it has access right.
  path : dirs.DocumentDir + '/path-to-file.anything'
})
.fetch('GET', 'http://www.example.com/file/example.zip', {
  //some headers ..
})
.then((res) => {
  // the path should be dirs.DocumentDir + 'path-to-file.anything'
  console.log('The file saved to ', res.path())
})

These files won't be removed automatically, please refer to Cache File Management

Upload example : Dropbox files-upload API

react-native-fetch-blob will convert the base64 string in body to binary format using native API, this process is done in a separated thread so that it won't block your GUI.

RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
    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.  
  }, base64ImageString)
  .then((res) => {
    console.log(res.text())
  })
  .catch((err) => {
    // error handling ..
  })

Upload a file from storage

If you're going to use a file as request body, just wrap the path with wrap API.

RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
    // dropbox upload headers
    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',
    // Change BASE64 encoded data to a file path with prefix `RNFetchBlob-file://`.
    // Or simply wrap the file path with RNFetchBlob.wrap().
  }, RNFetchBlob.wrap(PATH_TO_THE_FILE))
  .then((res) => {
    console.log(res.text())
  })
  .catch((err) => {
    // error handling ..
  })

Multipart/form-data example: Post form data with file and data

In version >= 0.3.0 you can also post files with form data, just put an array in body, with elements have property name, data, and filename(optional).

Elements have property filename will be transformed into binary format, otherwise, it turns into utf8 string.

  RNFetchBlob.fetch('POST', 'http://www.example.com/upload-form', {
    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: binaryDataInBase64},
    // custom content type
    { name : 'avatar-png', filename : 'avatar-png.png', type:'image/png', data: binaryDataInBase64},
    // part file from storage
    { name : 'avatar-foo', filename : 'avatar-foo.png', type:'image/foo', data: RNFetchBlob.wrap(path_to_a_file)},
    // elements without property `filename` will be sent as plain text
    { name : 'name', data : 'user'},
    { name : 'info', data : JSON.stringify({
      mail : '[email protected]',
      tel : '12345678'
    })},
  ]).then((resp) => {
    // ...
  }).catch((err) => {
    // ...
  })

What if you want to append a file to form data? Just like upload a file from storage example, wrap data by wrap API (this feature is only available for version >= v0.5.0). On version >= 0.6.2, it is possible to set custom MIME type when appending a file to form data. But keep in mind when the file is large it's likely to crash your app. Please consider use other strategy (see #94).

  RNFetchBlob.fetch('POST', 'http://www.example.com/upload-form', {
    Authorization : "Bearer access-token",
    otherHeader : "foo",
    // this is required, otherwise it won't be process as a multipart/form-data request
    'Content-Type' : 'multipart/form-data',
  }, [
    // append field data from file path
    {
      name : 'avatar',
      filename : 'avatar.png',
      // Change BASE64 encoded data to a file path with prefix `RNFetchBlob-file://`.
      // Or simply wrap the file path with RNFetchBlob.wrap().
      data: RNFetchBlob.wrap(PATH_TO_THE_FILE)
    },
    {
      name : 'ringtone',
      filename : 'ring.mp3',
      // use custom MIME type
      type : 'application/mp3',
      // upload a file from asset is also possible in version >= 0.6.2
      data : RNFetchBlob.wrap(RNFetchBlob.fs.asset('default-ringtone.mp3'))
    }
    // elements without property `filename` will be sent as plain text
    { name : 'name', data : 'user'},
    { name : 'info', data : JSON.stringify({
      mail : '[email protected]',
      tel : '12345678'
    })},
  ]).then((resp) => {
    // ...
  }).catch((err) => {
    // ...
  })

Upload/Download progress

In version >= 0.4.2 it is possible to know the upload/download progress. After 0.7.0 IOS and Android upload progress are also supported.

  RNFetchBlob.fetch('POST', 'http://www.example.com/upload', {
      //... some headers,
      'Content-Type' : 'octet-stream'
    }, base64DataString)
    // listen to upload progress event
    .uploadProgress((written, total) => {
        console.log('uploaded', written / total)
    })
    // listen to download progress event
    .progress((received, total) => {
        console.log('progress', received / total)
    })
    .then((resp) => {
      // ...
    })
    .catch((err) => {
      // ...
    })

In 0.9.6, you can specify an object as the first argument which contains count and interval, to the frequency of progress event (this will be done in the native context a reduce RCT bridge overhead). Notice that count argument will not work if the server does not provide response content length.

  RNFetchBlob.fetch('POST', 'http://www.example.com/upload', {
      //... some headers,
      'Content-Type' : 'octet-stream'
    }, base64DataString)
    // listen to upload progress event, emit every 250ms
    .uploadProgress({ interval : 250 },(written, total) => {
        console.log('uploaded', written / total)
    })
    // listen to download progress event, every 10%
    .progress({ count : 10 }, (received, total) => {
        console.log('progress', received / total)
    })
    .then((resp) => {
      // ...
    })
    .catch((err) => {
      // ...
    })

Cancel Request

After 0.7.0 it is possible to cancel an HTTP request. Upon cancellation, it throws a promise rejection, be sure to catch it.

let task = RNFetchBlob.fetch('GET', 'http://example.com/file/1')

task.then(() => { ... })
    // handle request cancelled rejection
    .catch((err) => {
        console.log(err)
    })
// cancel the request, the callback function is optional
task.cancel((err) => { ... })

Drop-in Fetch Replacement

0.9.0

If you have existing code that uses whatwg-fetch(the official fetch), it's not necessary to replace them with RNFetchblob.fetch, you can simply use our Fetch Replacement. The difference between Official them is official fetch uses whatwg-fetch which wraps XMLHttpRequest polyfill under the hood. It's a great library for web developers, but does not play very well with RN. Our implementation is simply a wrapper of our fetch and fs APIs, so you can access all the features we provided.

See document and examples

Android Media Scanner, and Download Manager Support

If you want to make a file in External Storage becomes visible in Picture, Downloads, or other built-in apps, you will have to use Media Scanner or Download Manager.

Media Scanner

Media scanner scans the file and categorizes by given MIME type, if MIME type not specified, it will try to resolve the file using its file extension.

RNFetchBlob
    .config({
        // DCIMDir is in external storage
        path : dirs.DCIMDir + '/music.mp3'
    })
    .fetch('GET', 'http://example.com/music.mp3')
    .then((res) => RNFetchBlob.fs.scanFile([ { path : res.path(), mime : 'audio/mpeg' } ]))
    .then(() => {
        // scan file success
    })
    .catch((err) => {
        // scan file error
    })

Download Manager

When downloading large files on Android it is recommended to use Download Manager, it supports a lot of native features like the progress bar, and notification, also the download task will be handled by OS, and more efficient.

When using DownloadManager, fileCache and path properties in config will not take effect, because Android DownloadManager can only store files to external storage, also notice that Download Manager can only support GET method, which means the request body will be ignored.

When download complete, DownloadManager will generate a file path so that you can deal with it.

RNFetchBlob
    .config({
        addAndroidDownloads : {
            useDownloadManager : true, // <-- this is the only thing required
            // Optional, override notification setting (default to true)
            notification : false,
            // Optional, but recommended since android DownloadManager will fail when
            // the url does not contains a file extension, by default the mime type will be text/plain
            mime : 'text/plain',
            description : 'File downloaded by download manager.'
        }
    })
    .fetch('GET', 'http://example.com/file/somefile')
    .then((resp) => {
      // the path of downloaded file
      resp.path()
    })

Your app might not have right to remove/change the file created by Download Manager, therefore you might need to set custom location to the download task.

Download Notification and Visibility in Download App (Android Only)

If you need to display a notification upon the file is downloaded to storage (as the above) or make the downloaded file visible in "Downloads" app. You have to add some options to config.

RNFetchBlob.config({
  fileCache : true,
  // android only options, these options be a no-op on IOS
  addAndroidDownloads : {
    // Show notification when response data transmitted
    notification : true,
    // Title of download notification
    title : 'Great ! Download Success ! :O ',
    // File description (not notification description)
    description : 'An image file.',
    mime : 'image/png',
    // Make the file scannable  by media scanner
    mediaScannable : true,
  }
})
.fetch('GET', 'http://example.com/image1.png')
.then(...)

Open Downloaded File with Intent

This is a new feature added in 0.9.0 if you're going to open a file path using official Linking API that might not work as expected, also, if you're going to install an APK in Downloads app, that will not function too. As an alternative, you can try actionViewIntent API, which will send an ACTION_VIEW intent for you which uses the given MIME type.

Download and install an APK programmatically

const android = RNFetchBlob.android

RNFetchBlob.config({
    addAndroidDownloads : {
      useDownloadManager : true,
      title : 'awesome.apk',
      description : 'An APK that will be installed',
      mime : 'application/vnd.android.package-archive',
      mediaScannable : true,
      notification : true,
    }
  })
  .fetch('GET', `http://www.example.com/awesome.apk`)
  .then((res) => {
      android.actionViewIntent(res.path(), 'application/vnd.android.package-archive')
  })

Or show an image in image viewer

      android.actionViewIntent(PATH_OF_IMG, 'image/png')

File System

File Access

File access APIs were made when developing v0.5.0, which helping us write tests, and was not planned to be a part of this module. However, we realized that it's hard to find a great solution to manage cached files, everyone who uses this module may need these APIs for their cases.

Before start using file APIs, we recommend read Differences between File Source first.

File Access APIs

See File API for more information

File Stream

In v0.5.0 we've added writeStream and readStream, which allows your app read/write data from the file path. This API creates a file stream, rather than convert entire data into BASE64 encoded string. It's handy when processing large files.

When calling readStream method, you have to open the stream, and start to read data. When the file is large, consider using an appropriate bufferSize and interval to reduce the native event dispatching overhead (see Performance Tips)

The file stream event has a default throttle(10ms) and buffer size which preventing it cause too much overhead to main thread, yo can also tweak these values.

let data = ''
RNFetchBlob.fs.readStream(
    // file path
    PATH_TO_THE_FILE,
    // encoding, should be one of `base64`, `utf8`, `ascii`
    'base64',
    // (optional) buffer size, default to 4096 (4095 for BASE64 encoded data)
    // when reading file in BASE64 encoding, buffer size must be multiples of 3.
    4095)
.then((ifstream) => {
    ifstream.open()
    ifstream.onData((chunk) => {
      // when encoding is `ascii`, chunk will be an array contains numbers
      // otherwise it will be a string
      data += chunk
    })
    ifstream.onError((err) => {
      console.log('oops', err)
    })
    ifstream.onEnd(() => {  
      <Image source={{ uri : 'data:image/png,base64' + data }}
    })
})

When using writeStream, the stream object becomes writable, and you can then perform operations like write and close.

RNFetchBlob.fs.writeStream(
    PATH_TO_FILE,
    // encoding, should be one of `base64`, `utf8`, `ascii`
    'utf8',
    // should data append to existing content ?
    true)
.then((ofstream) => {
    ofstream.write('foo')
    ofstream.write('bar')
    ofstream.close()
})

Cache File Management

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

  // remove file using RNFetchblobResponse.flush() object method
  RNFetchblob.config({
      fileCache : true
    })
    .fetch('GET', 'http://example.com/download/file')
    .then((res) => {
      // remove cached file from storage
      res.flush()
    })

  // remove file by specifying a path
  RNFetchBlob.fs.unlink('some-file-path').then(() => {
    // ...
  })

You can also group requests by using session API and use dispose to remove them all when needed.

  RNFetchblob.config({
    fileCache : true
  })
  .fetch('GET', 'http://example.com/download/file')
  .then((res) => {
    // set session of a response
    res.session('foo')
  })  

  RNFetchblob.config({
    // you can also set session beforehand
    session : 'foo'
    fileCache : true
  })
  .fetch('GET', 'http://example.com/download/file')
  .then((res) => {
    // ...
  })  

  // or put an existing file path to the session
  RNFetchBlob.session('foo').add('some-file-path')
  // remove a file path from the session
  RNFetchBlob.session('foo').remove('some-file-path')
  // list paths of a session
  RNFetchBlob.session('foo').list()
  // remove all files in a session
  RNFetchBlob.session('foo').dispose().then(() => { ... })

Transfer Encoding

After 0.9.4, the Chunked transfer encoding is disabled by default due to some service provider may not support chunked transfer. To enable it, set Transfer-Encoding header to Chunked.

RNFetchBlob.fetch('POST', 'http://example.com/upload', { 'Transfer-Encoding' : 'Chunked' }, bodyData)

Self-Signed SSL Server

By default, react-native-fetch-blob does NOT allow connection to unknown certification provider since it's dangerous. To connect a server with self-signed certification, you need to add trusty to config explicitly. This function is available for version >= 0.5.3

RNFetchBlob.config({
  trusty : true
})
.then('GET', 'https://mysite.com')
.then((resp) => {
  // ...
})

Web API Polyfills

After 0.8.0 we've made some Web API polyfills that makes some browser-based library available in RN.

  • Blob
  • XMLHttpRequest (Use our implementation if you're going to use it with Blob)

Here's a sample app that uses polyfills to upload files to FireBase.

Performance Tips

Read Stream and Progress Event Overhead

If the process seems to block JS thread when file is large when reading data via fs.readStream. It might because the default buffer size is quite small (4kb) which result in a lot of events triggered from JS thread. Try to increase the buffer size (for example 100kb = 102400) and set a larger interval (available for 0.9.4+, the default value is 10ms) to limit the frequency.

Reduce RCT Bridge and BASE64 Overhead

React Native connects JS and Native context by passing JSON around React Native bridge, and there will be an overhead to convert data before they sent to each side. When data is large, this will be quite a performance impact to your app. It's recommended to use file storage instead of BASE64 if possible.The following chart shows how much faster when loading data from storage than BASE64 encoded string on iPhone 6.

ASCII Encoding has /terrible Performance

Due to the lack of typed array implementation in JavascriptCore, and limitation of React Native structure, to convert data to JS byte array spends lot of time. Use it only when needed, the following chart shows how much time it takes when reading a file with different encoding.

Concat and Replacing Files

If you're going to concatenate files, you don't have to read the data to JS context anymore! In 0.8.0 we introduced new encoding uri for writeFile and appendFile API, which make it possible to handle the whole process in native.

Caveats

  • This library does not urlencode unicode characters in URL automatically, see #146.
  • When you create a Blob , from an existing file, the file WILL BE REMOVED if you close the blob.
  • If you replaced window.XMLHttpRequest for some reason (e.g. make Firebase SDK work), it will also affect how official fetch works (basically it should work just fine).
  • When file stream and upload/download progress event slow down your app, consider an upgrade to 0.9.6+, use additional arguments to limit its frequency.
  • When passing a file path to the library, remove file:// prefix.

when you got a problem, have a look at Trouble Shooting or issues labeled Trouble Shooting, there'd be some helpful information.

Changes

See release notes

Development

If you're interested in hacking this module, check our development guide, there might be some helpful information. Please feel free to make a PR or file an issue.

react-native-fetch-blob's People

Contributors

adbl avatar ainoya avatar atlanteh avatar bcpclone avatar bronco avatar follower avatar francisco-sanchez-molina avatar grylance avatar hhravn avatar jbrodriguez avatar jeremistadler avatar jsm avatar jungledruid avatar kaishley avatar kejinliang avatar lll000111 avatar maxpowa avatar mdmush avatar mgiachetti avatar mikemonteith avatar moschan avatar ncnlinh avatar npomfret avatar pedramsaleh avatar rghorbani avatar simongomez95 avatar smartt avatar steveliles avatar timsuchanek avatar wkh237 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-native-fetch-blob's Issues

Error: `_reactNativeFetchBlob2.default.readStream is not a function`

I'm trying to use readStream, but react native is giving me the red-screen-of-death with the message, "_reactNativeFetchBlob2.default.readStream is not a function".

The package was installed using npm and linked using rnpm. And ideas?

Versions:

  • "react-native": "0.28.0"
  • "react-native-fetch-blob": "^0.6.1"
  • iPhone 6 simulator running iOS 9.3
  • XCode 7.3.1

Save downloaded blob data to file system directly

The module now transforms downloaded data into BASE64 string in native context, but it will be inefficient if the developer intend to save the data into storage directly.

Perhaps there should be an option for make it more applicable.

  • Android implementation
  • IOS implementation
  • Test cases
  • Documentation

[iOS] Can't build without ARC errors

RNFetchBlobNetwork.m:32:13: ARC forbids synthesizing a property of an Objective-C object with unspecified ownership or storage attribute
RNFetchBlobNetwork.m:35:13: ARC forbids synthesizing a property of an Objective-C object with unspecified ownership or storage attribute
RNFetchBlobNetwork.m:36:13: ARC forbids synthesizing a property of an Objective-C object with unspecified ownership or storage attribute
RNFetchBlobFS.h:18:28: Existing instance variable 'callback' for property 'callback' with unsafe_unretained attribute must be __unsafe_unretained

I'm using 0.5.3.
Is it because I've added the project manually rather then with rnpm and have missed some build flags?

HTTP request should be able to cancel and resume

Related to #37 , cancel mechanism will be added into 0.7.0.

resume can be achieved by simply using Range header, and appendFile function. That might be an inefficient way but we will improve that after #56 .

  • Android part (cancel request)
  • IOS part (cancel request)
  • test cases
  • documentation

.progress() not working

Are you sure this works? As in my case it doesn't seem to enter in the functions and jumps directly to .then()/.catch(). It waited for the file to upload but never executed anything inside the .progress().
Or may be you could provide an example(point me to it) on how to use it.
Btw, thanks, I'm new to RN and iOS, when nothing else seemed to work, rnfb worked well.
here:
trySendingImageToServer(url,base64ImageString,tryNum){
RNFetchBlob.fetch('POST',url,{
'Authorization' : 'Basic YWRtaW46MXEydzNlNHIlVA==',
},base64ImageString)
.progress((received,total)=>{
console.log('progress',received/total);
})
.then((resp)=>{
console.log("Response : ",resp.text());
})
.catch((err)=>{
console.log("Error : ",err);
if (tryNum>0){
this.trySendingImageToServer(url,base64ImageString,tryNum-1);
console.log("Trying again.. "+tryNum);
}
else{
console.log("Not trying anymore");
Alert.alert('Please check your connection');
}
});
}

Implement Blob and other polyfills

According to #37 advice from @vaibhawc , perhaps we should consider implement Blob class on RN since we already have network and fs APIs. This may let some JS libraries that depends on web API becomes available in RN. At least we can try to make firebase.storage work.

Work in progress document

Spec Reference

These are polyfills that planned to be implemented so far

  • Blob
  • File
  • XMLHttpRequest
  • FileReader (postponed)
  • Test cases
  • Documentation

This feature will develop on branch 0.8.0

v0.5.0 release preparation

v0.5.0 features are almost finished, before it release we still got something to do.

  • Documentation
  • More test cases
  • System directory API improvement
  • Memory and file cache release

go into error except in chrome debugging

I use fetch to post a base64 image data and always go into error in debug or release. when i open debug js, it success. what's wrong. is it anything i have to setting?

[Android] undefined is not an object (evaluating 'RNFetchBlob.DocumentDir')

Hello!

This error appears at start launch in the simulator/device.

In my example I'm trying implement this part of code, how we can avoid this issue?

Thank you in advance.

componentDidMount() { RNFetchBlob .config({ // add this option that makes response data to be stored as a file, // this is much more performant. fileCache : true, }) .fetch('GET', 'https://homepages.cae.wisc.edu/~ece533/images/mountain.png') .then((res) => { // the temp file path console.log('The file saved to ', res.path('/')) }) requestPermission("android.permission.ACCESS_FINE_LOCATION").then((result) => { console.log("Granted!", result);// now you can set the listenner to watch the user geo location }, (result) => { console.log("Not Granted!"); console.log(result); });

[email protected]
[email protected]
[email protected]
[email protected]

img1
img2

Move API document to wiki

The README.md has to much content, I think it would be better to leave recipes in README.md and move API references to wiki.

[Android] Switch to OKHttp

React-Native implements fetch API using OKHTTP, we should consider use it.

TODOs

  • Android content URI read problem
  • Multipart content length correctness
  • Documentation

New fs APIs make file concat and replace more efficient

Consider the following scenario

If we wish to concat two files or replace content of one file by another, we have to cache the file content in JS context and then write the data to destination.

const fs = RNFetchBlob.fs
fs.readFile(FILE_SRC, 'base64')
     .then((b64data) => fs.appendFile(FILE_DEST, b64data, 'base64'))
     .then(() => {  console.log(`concated ${FILE_SRC} to ${FILE_DEST}`) })

This seems not efficient especially when the file is big, that cache will likely consume up the available memory.

Therefore, I think we should make writeStream, writeFile, and appendFile accept a file path as their input, and then we can do as follow.

const fs = RNFetchBlob.fs
fs.appendFile(FILE_DEST, FILE_SRC, 'uri')
     .then(() => {  console.log(`concated ${FILE_SRC} to ${FILE_DEST}`) })
  • Add uri encoding type to writeFile
  • Add uri encoding type to appendFile
  • Test cases

Responses come back but callback is never called

Hey, I'm seeing a strange issue on Android when I try to use this lib.
screen shot 2016-06-14 at 9 59 44 am

I'm working on an application that needs to download images from the web, and store them on device. However, as you can see, the process is completed by AsyncHTTP, but the callback itself is never called.

My code:

RNFetchBlob
.config({
fileCache: true,
path : fileName
})
.fetch('GET', tile.url, {})
.then((res) => {
console.log("Saved to " + res.path())
processedTiles++;
if (processedTiles >= tilesToBeProcessed) {
parent.onTileProcessed(card, index);
}
});

Seperate upload and download progress event

IOS supports both upload and download progress, but there's only one way to register progress event handler, this will cause unexpected progress event triggering on IOS.

  • implement android upload progress in 0.7.0

Android runtime permissions

Hi,

Android 23 runs with runtime permissions, so if you want to write to the downloads folder for example you don't just state that in the manifest file, you also have to have a runtime check and confirmation from the user.

https://developer.android.com/training/permissions/requesting.html

I can't see that how this has been implemented in this module, if it hasn't due to being outside of scope / requirements then does anyone have any advice on how to apply this. Due to the nature of the module here I would think it would be very useful to add to the Read me.

Thanks

[IOS] NSURLSession support

When app turned into background, HTTP connections will be interrupt. This would be a problem when transmitting large data.

Undefined error

Hi,

I want to use your package for fetching network images.
The images are protected by Basic Auth.

I would like to use them in an component, but keep getting following error on RNFetchBlob.fetch():

TypeError: Cannot read property 'fetchBlob' of undefined

I'm on RN ^0.22.2
i've installed rnpm and ran the rnpm link command

Any clue on what i'm missing here?

Integration test

  • octet-stream upload
  • octet-stream download
  • multipart/form-data upload
  • multipart/form-data download
  • progress report API
  • request redirection
  • file access API

Allow setting custom Content-Type within multi-part forms

I had a case when working with an API where I needed to specifically set the Content-Type (from "application/octet-stream" to "image/jpeg". As a quick hack, I implemented it this way [on the feat_45 branch]:

diff --git a/src/ios/RNFetchBlobReqBuilder.m b/src/ios/RNFetchBlobReqBuilder.m
index df4c3d1..6b44b74 100644
--- a/src/ios/RNFetchBlobReqBuilder.m
+++ b/src/ios/RNFetchBlobReqBuilder.m
@@ -134,6 +134,7 @@
         {
             NSString * name = [field valueForKey:@"name"];
             NSString * content = [field valueForKey:@"data"];
+            NSString * contentType = [field valueForKey:@"type"];
             // field is a text field
             if([field valueForKey:@"filename"] == nil || content == [NSNull null]) {
                 [formData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
@@ -155,7 +156,7 @@
                             NSString * filename = [field valueForKey:@"filename"];
                             [formData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                             [formData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, filename] dataUsingEncoding:NSUTF8StringEncoding]];
-                            [formData appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
+                            [formData appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]];
                             [formData appendData:content];
                             [formData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
                             i++;
@@ -174,7 +175,7 @@
                 NSString * filename = [field valueForKey:@"filename"];
                 [formData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                 [formData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, filename] dataUsingEncoding:NSUTF8StringEncoding]];
-                [formData appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
+                [formData appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]];
                 [formData appendData:blobData];
                 [formData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
             }

..and used it like this:

            RNFetchBlob.fetch('POST', url, {
                Authorization: `Bearer ${token}`,
                Accept: "application/json",
                "Content-Type": "multipart/form-data",
            }, [
                {
                    name: 'image',
                    filename: filename,
                    data: RNFetchBlob.wrap(asset.uri),
                    "type": 'image/jpeg',
                },
                {
                    name: "someOtherField",
                    data: JSON.stringify({foo: 1, bar: 2}),
                    "type": "application/json",
                },

I figured you might want to implement it different so that it's optional, and works on Android, so no PR currently :-)

[Android] Download Manager support

Download Manager provides better UX for Android users, we should add support for this.

Also, download file using download manager can avoid problems when app is in suspended.

  • Download notification
  • Download using download manager API
  • Test cases

Add config API

there will be lot of features in the future, so that current API needs to be extend, but how?

Compatible to ES5

Refer to #58 , since the module uses ES6 style export, developers who uses require('react-native-fetch-blob') will not be able to use the module directly. Though they can still uses by using default property, but it would be better if the module can be used more easily.

Network optimization (don't re-download if present in cache)

Hi @wkh237, awesome library, much needed in React Native.

My suggestion is:

  • if fileCache is set to true
  • when a fetch is issued, check if that url has already been downloaded
  • if it has, return the path
  • if it hasn't, request from the server as normal

Let me know if you will pursue such a change, in any case I'll take a look at the code and see if I can come up with something (at least on the Android side)

Self signed certificates

Hi. I tried to use your library and get a problem if the site uses self signed certificate. Do you have any solution for that?

upload doesn't work

I'm getting the error:

sorry, the request failed. Please try again. {line: 381, column: 882, sourceURL: file: //index.android.bundle.

Not really sure what the error is all about but it seems I do not get this error when I enable chrome debugging. I tried creating a deploy version of the app in hopes that it would work but it didn't.

I'll come back later to update and provide more info.

Support for iOS assets-library:// files

It's unclear from the docs whether or not this library can be used to upload images from an iOS user's camera roll. The React Native camera roll API returns file URIs like, assets-library://asset/asset.JPG?id=1234-5678-ABCDE-3210&ext=JPG. Should I be able to use RNFetchBlob.wrap() on such a URI, like:

RNFetchBlob.fetch('POST', 'http://www.example.com/upload-form', {
    Authorization : "Bearer access-token",
    'Content-Type' : 'multipart/form-data',
  }, [
    // append field data from file path
    {
      name : 'avatar',
      filename : 'avatar.png',
      data: RNFetchBlob.wrap("assets-library://asset/asset.JPG?id=1234-5678-ABCDE-3210&ext=JPG")
    }
...

Make read/write file API easier to use

The only way to read/write file is using file stream, there should be some handy API make this easier.

for example

fs.readFile(path-to-file).then((data) => {
    // ...
})

fs.writeFile(path-to-file).then((data) => {
    // ...
})
  • IOS implementation
  • Android implementation
  • Test cases

Changes that slowed down download?

Hi guys,

I'm using this library to download a massive amount of separate files to disk (1k+ per load). It's a mapping application, and so this is necessary.

Since the latest version (I upgraded from 0.4.2 to 0.5.2) downloads have slowed down significantly. Any suggestions where I can look to fix this? It almost seems like the requests are now syncrhonous instead of asynchronous. Unable to perform multiple requests at the same time?

Thanks!

File cache management

In upcoming version v0.5.0, new APIs allow RN app access file system directly, is there a better solution to remove cached files than just manually calling unlink API ?

  • session API implementation

Library doesn't compile with project on IOS

I get the below error message when I try to compile my project in IOS. The project works fine on android. I have followed the steps as given in the installation section.
screen shot 2016-07-01 at 7 15 41 pm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.