react-native-fileupload 使用

react-native-fileupload 使用

简介

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

  1. 支持同时上传多个文件
  2. 支持文件和文件夹
安装
npm install react-native-fileupload --save
配置

1. ios

  1. 在xcode 中 右击你的项目 ➜ Add Files to [your project's name]

    markdown

  2. 选择 node_modules ➜ react-native-fileupload and add FileUpload.m markdown

  3. 重新run一下项目

  4. 这这里我遇到了

     RCTBridgeModule.h file not found
    

    解决参看:链接

2. Android

Note: Android support requires React Native 0.12 or later

  1. 编辑 android/settings.gradle

     rootProject.name = 'MyApp'
     include ':app'
    
     //添加下面两句:
    
     include ':react-native-fileupload'
     project(':react-native-fileupload').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-fileupload/android')
    
  2. 编辑android/app/build.gradle 如下

     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.+'
    
       // 添加这一句:
       compile project(':react-native-fileupload')
     }
    
  3. 编辑 MainActivity.java 文件在 (android/app/src/main/java/.../MainActivity.java)如下面

     package com.xjcloudprj;
    
     //添加这一句
     import com.yoloci.fileupload.FileUploadPackage;
    
     import com.facebook.react.ReactActivity;
    
     public class MainActivity extends ReactActivity {
    
         /**
          * Returns the name of the main component registered from JavaScript.
          * This is used to schedule rendering of the component.
          */
         [@Override](https://my.oschina.net/u/1162528)
         protected String getMainComponentName() {
             return "XJCloudPrj";
         }
     }
    
使用

这里自己去研究吧,为了文章的完整行,我就把git上的列子抄下来了:

'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);	

转载于:https://my.oschina.net/KJhulinhua/blog/1002636

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值