Git Product home page Git Product logo

react-native-background-upload's Introduction

react-native-background-upload npm version

The only React Native http post file uploader with android and iOS background support. If you are uploading large files like videos, use this so your users can background your app during a long upload.

NOTE: Use major version 4 with RN 47.0 and greater. If you have RN less than 47, use 3.0. To view all available versions: npm show react-native-background-upload versions

Installation

1. Install package

npm install --save react-native-background-upload

or

yarn add react-native-background-upload

Note: if you are installing on React Native < 0.47, use [email protected] instead of react-native-background-upload

2. Link Native Code

Automatic Native Library Linking

react-native link react-native-background-upload

Or, Manually Link It

iOS

  1. In the XCode's "Project navigator", right click on your project's Libraries folder ➜ Add Files to <...>
  2. Go to node_modulesreact-native-background-uploadios ➜ select VydiaRNFileUploader.xcodeproj
  3. Add VydiaRNFileUploader.a to Build Phases -> Link Binary With Libraries

Android

  1. Add the following lines to android/settings.gradle:

    include ':react-native-background-upload'
    project(':react-native-background-upload').projectDir = new File(settingsDir, '../node_modules/react-native-background-upload/android')
  2. Add the compile and resolutionStrategy line to the dependencies in android/app/build.gradle:

    configurations.all { resolutionStrategy.force 'com.squareup.okhttp3:okhttp:3.4.1' } // required by react-native-background-upload until React Native supports okhttp >= okhttp 3.5
    
    dependencies {
        compile project(':react-native-background-upload')
    }
  3. Add the import and link the package in MainApplication.java:

    import com.vydia.RNUploader.UploaderReactPackage;  <-- add this import
    
    public class MainApplication extends Application implements ReactApplication {
        @Override
        protected List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(
                new MainReactPackage(),
                new UploaderReactPackage() // <-- add this line
            );
        }
    }
  4. Ensure Android SDK versions. Open your app's android/app/build.gradle file. Ensure compileSdkVersion and targetSdkVersion are 25. Otherwise you'll get compilation errors.

3. Expo

To use this library with Expo one must first detach (eject) the project and follow the normal react-native link instructions. Additionally on iOS there is a must to add a Header Search Path to other dependencies which are managed using Pods. To do so one has to add $(SRCROOT)/../../../ios/Pods/Headers/Public to Header Search Path in VydiaRNFileUploader module using XCode.

Usage

import Upload from 'react-native-background-upload'

const options = {
  url: 'https://myservice.com/path/to/post',
  path: 'file://path/to/file/on/device',
  method: 'POST',
  type: 'raw',
  headers: {
    'content-type': 'application/octet-stream', // Customize content-type
    'my-custom-header': 's3headervalueorwhateveryouneed'
  },
  // Below are options only supported on Android
  notification: {
    enabled: true
  }
}

Upload.startUpload(options).then((uploadId) => {
  console.log('Upload started')
  Upload.addListener('progress', uploadId, (data) => {
    console.log(`Progress: ${data.progress}%`)
  })
  Upload.addListener('error', uploadId, (data) => {
    console.log(`Error: ${data.error}%`)
  })
  Upload.addListener('cancelled', uploadId, (data) => {
    console.log(`Cancelled!`)
  })
  Upload.addListener('completed', uploadId, (data) => {
    // data includes responseCode: number and responseBody: Object
    console.log('Completed!')
  })
}).catch((err) => {
  console.log('Upload error!', err)
})

Multipart Uploads

Just set the type option to multipart and set the field option. Example:

const options = {
  url: 'https://myservice.com/path/to/post',
  path: 'file://path/to/file%20on%20device.png',
  method: 'POST',
  field: 'uploaded_media',
  type: 'multipart'
}

Note the field property is required for multipart uploads.

API

Top Level Functions

All top-level methods are available as named exports or methods on the default export.

startUpload(options)

The primary method you will use, this starts the upload process.

Returns a promise with the string ID of the upload. Will reject if there is a connection problem, the file doesn't exist, or there is some other problem.

options is an object with following values:

Note: You must provide valid URIs. react-native-background-upload does not escape the values you provide.

Name Type Required Default Description Example
url string Required URL to upload to https://myservice.com/path/to/post
path string Required File path on device file://something/coming/from%20the%20device.png
type 'raw' or 'multipart' Optional raw Primary upload type.
method string Optional POST HTTP method
customUploadId string Optional startUpload returns a Promise that includes the upload ID, which can be used for future status checks. By default, the upload ID is automatically generated. This parameter allows a custom ID to use instead of the default.
headers object Optional HTTP headers { 'Accept': 'application/json' }
field string Required if type: 'multipart' The form field name for the file. Only used when type: 'multipart uploaded-file
parameters object Optional Additional form fields to include in the HTTP request. Only used when type: 'multipart
notification object with single enabled field Optional Android only. { enabled: false }

getFileInfo(path)

Returns some useful information about the file in question. Useful if you want to set a MIME type header.

path is a string, such as file://path.to.the.file.png

Returns a Promise that resolves to an object containing:

Name Type Required Description Example
name string Required The file name within its directory. image2.png
exists boolean Required Is there a file matching this path?
size number If exists File size, in bytes
extension string If exists File extension mov
mimeType string If exists The MIME type for the file. video/mp4

cancelUpload(uploadId)

Cancels an upload.

uploadId is the result of the Promise returned from startUpload

Returns a Promise that resolves to an boolean indicating whether the upload was cancelled.

addListener(eventType, uploadId, listener)

Adds an event listener, possibly confined to a single upload.

eventType Event to listen for. Values: 'progress' | 'error' | 'completed' | 'cancelled'

uploadId The upload ID from startUpload to filter events for. If null, this will include all uploads.

listener Function to call when the event occurs.

Events

progress

Event Data

Name Type Required Description
id string Required The ID of the upload.
progress 0-100 Required Percentage completed.

error

Event Data

Name Type Required Description
id string Required The ID of the upload.
error string Required Error message.

completed

Event Data

Name Type Required Description
id string Required The ID of the upload.
responseCode string Required HTTP status code received
responseBody string Required HTTP response body

cancelled

Event Data

Name Type Required Description
id string Required The ID of the upload.

FAQs

Is there an example/sandbox app to test out this package?

Yes, there is a simple react native app that comes with an express server where you can see react-native-background-upload in action and try things out in an isolated local environment.

ReactNativeBackgroundUploadExample

Does it support iOS camera roll assets?

Yes, as of version 4.3.0.

Does it support multiple file uploads?

Yes and No. It supports multiple concurrent uploads, but only a single upload per request. That should be fine for 90%+ of cases.

Why should I use this file uploader instead of others that I've Googled like react-native-uploader?

This package has two killer features not found anywhere else (as of 12/16/2016). First, it works on both iOS and Android. Others are iOS only. Second, it supports background uploading. This means that users can background your app and the upload will continue. This does not happen with other uploaders.

Contributing

See CONTRIBUTING.md.

Common Issues

BREAKING CHANGE IN 3.0

This is for 3.0 only. This does NOT apply to 4.0, as recent React Native versions have upgraded the okhttp dependencies. Anyway...

In 3.0, you need to add

    configurations.all { resolutionStrategy.force 'com.squareup.okhttp3:okhttp:3.4.1' }

to your app's app's android/app/build.gradle file.

Just add it above (not within) dependencies and you'll be fine.

BREAKING CHANGE IN 2.0

Two big things happened in version 2.0. First, thehe Android package name had to be changed, as it conflicted with our own internal app. My bad. Second, we updated the android upload service dependency to the latest, but that requires the app have a compileSdkVersion and targetSdkVersion or 25.

To upgrade: In MainApplication.java:

Change

```java
import com.vydia.UploaderReactPackage;
```

to

```java
import com.vydia.RNUploader.UploaderReactPackage;
```

Then open your app's android/app/build.gradle file. Ensure compileSdkVersion and targetSdkVersion are 25.

Done!

Known issues

Android APK 27 and above require notifications to provide their own NotificationChannel. react-native-background-upload does not yet meet this requirement and thus will cause crashes in Android 8.1 and above. This issue can be avoided by electing not to use native notifications.

const options = {
  // ...
  notification: {
    enabled: false,
  },
};

Gratitude

Thanks to:

react-native-background-upload's People

Contributors

stevepotter avatar ovr avatar tsmmark avatar rkretzschmar avatar kenleezle avatar watadarkstar avatar ejohnf avatar michelbahl avatar dtuite avatar thatjuan avatar

Watchers

Mayank Kumar avatar James Cloos avatar Lynn avatar  avatar  avatar Nacho avatar Mathieu Rul avatar  avatar Matt Harper avatar Guille avatar  avatar gbbg avatar Luigi Di Grande avatar Shwetha Gopalan avatar Leon Zuo avatar Satheesh Ramamoorthy avatar  avatar  avatar Thiruveni Annam avatar Debora Batista avatar

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.