GithubHelp home page GithubHelp logo

mobileemotion / react-native-fileupload Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jason-ong/react-native-fileupload

0.0 1.0 0.0 33 KB

Multi-form file upload for react-native

License: MIT License

Java 62.37% Objective-C 37.63%

react-native-fileupload's Introduction

react-native-fileupload NPM version

Important: iOS version created by booxood (react-native-file-upload). This repository is the continuation of https://github.com/booxood/react-native-file-upload.

  • Support to upload multiple files at a time
  • Support to files and fields

Getting started

npm install react-native-fileupload --save

iOS

  1. In XCode, in the project navigator, right click your project โžœ Add Files to [your project's name]
  2. Go to node_modules โžœ react-native-fileupload and add FileUpload.m
  3. Run your project (Cmd+R)

Android

Note: Android support requires React Native 0.12 or later

  • Edit android/settings.gradle to look like this:

    rootProject.name = 'MyApp'
    
    include ':app'
    
    //Add the following two lines:
    include ':react-native-fileupload'
    project(':react-native-fileupload').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-fileupload/android')
    
  • Edit android/app/build.gradle (note: app folder) to look like this:

    apply plugin: 'com.android.application'
    
    android {
      ...
    }
    
    dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      compile 'com.android.support:appcompat-v7:23.0.0'
      compile 'com.facebook.react:react-native:0.12.+'
    
      // Add this line:
      compile project(':react-native-fileupload')
    }
    
  • Edit your MainActivity.java (deep in android/app/src/main/java/...) to look like this:

    package com.myapp;
    
    // Add this line:
    import com.yoloci.fileupload.FileUploadPackage;
    
    import android.app.Activity;
    ....
    
    public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
    
      private ReactInstanceManager mReactInstanceManager;
      private ReactRootView mReactRootView;
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mReactRootView = new ReactRootView(this);
    
        mReactInstanceManager = ReactInstanceManager.builder()
          .setApplication(getApplication())
          .setBundleAssetName("index.android.bundle")
          .setJSMainModuleName("index.android")
          .addPackage(new MainReactPackage())
    
          // and this line:
          .addPackage(new FileUploadPackage())
    
          .setUseDeveloperSupport(BuildConfig.DEBUG)
          .setInitialLifecycleState(LifecycleState.RESUMED)
          .build();
    
        mReactRootView.startReactApplication(mReactInstanceManager, "MyApp", null);
    
        setContentView(mReactRootView);
      }
      ...
    }
    

Usage

All you need is to export module var FileUpload = require('NativeModules').FileUpload; and direct invoke FileUpload.upload.

'use strict';

var React = require('react-native');
var FileUpload = require('NativeModules').FileUpload;

var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
} = React;

var FileUploadDemo = React.createClass({
  componentDidMount: function() {
    var obj = {
        uploadUrl: 'http://127.0.0.1:3000',
        method: 'POST', // default 'POST',support 'POST' and 'PUT'
        headers: {
          'Accept': 'application/json',
        },
        fields: {
            'hello': 'world',
        },
        files: [
          {
            name: 'one', // optional, if none then `filename` is used instead
            filename: 'one.w4a', // require, file name
            filepath: '/xxx/one.w4a', // require, file absoluete path
            filetype: 'audio/x-m4a', // options, if none, will get mimetype from `filepath` extension
          },
        ]
    };
    FileUpload.upload(obj, function(err, result) {
      console.log('upload:', err, result);
    })
  },
  render: function() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
      </View>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
});

AppRegistry.registerComponent('FileUploadDemo', () => FileUploadDemo);

License

MIT

react-native-fileupload's People

Contributors

jason-ong avatar lvbingru avatar maluramichael avatar philippkrone avatar venepe avatar

Watchers

 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.