React Native 文档查看组件

本文原创首发于公众号:ReactNative开发圈,转载需注明出处。

React Native 文档查看组件:react-native-doc-viewer,可以在手机上直接打开文档,支持远程和本地文档。支持的文档格式:xls,ppt,doc,xlsx,pptx,docx,png,jpg,pdf,mp4。支持iOS和Android。

安装方法

 
 
  1. npm install react-native-doc-viewer --save

  2. react-native link react-native-doc-viewer

API说明

  • openDoc 打开远程货或本地文档

  • openDocb64 打开Base64字符串格式文档

  • openDocBinaryinUrl 打开二进制文件

  • playMovie 打开视频文件

使用示例

 
 
  1. import React, { Component } from 'react';

  2. import {

  3.    AppRegistry,

  4.    StyleSheet,

  5.    Text,

  6.    View,

  7.    Platform,

  8.    Button,

  9.    Alert,

  10.    ActivityIndicator

  11. } from 'react-native';

  12. import OpenFile from 'react-native-doc-viewer';

  13. var RNFS = require('react-native-fs');

  14. var SavePath = Platform.OS === 'ios' ? RNFS.MainBundlePath : RNFS.DocumentDirectoryPath;

  15. export default class Component02 extends Component {

  16.    static navigationOptions = ({ navigation }) => ({

  17.        title: `${navigation.state.params.name}`,

  18.    });

  19.    state = { animating: false }

  20.    /*

  21.    * Handle WWW File Method

  22.    * fileType Default == "" you can use it, to set the File Extension (pdf,doc,xls,ppt etc) when in the Url the File Extension is missing.

  23.    */

  24.    handlePress = () => {

  25.        this.setState({ animating: true });

  26.        if (Platform.OS === 'ios') {

  27.            OpenFile.openDoc([{

  28.                url: "https://calibre-ebook.com/downloads/demos/demo.docx",

  29.                fileNameOptional: "test filename"

  30.            }], (error, url) => {

  31.                if (error) {

  32.                    this.setState({ animating: false });

  33.                } else {

  34.                    this.setState({ animating: false });

  35.                    console.log(url)

  36.                }

  37.            })

  38.        } else {

  39.            //Android

  40.            this.setState({ animating: true });

  41.            OpenFile.openDoc([{

  42.                url: "https://www.huf-haus.com/fileadmin/Bilder/Header/ART_3/Header_HUF_Haus_ART_3___1_.jpg", // Local "file://" + filepath

  43.                fileName: "sample",

  44.                cache: false,

  45.                fileType: "jpg"

  46.            }], (error, url) => {

  47.                if (error) {

  48.                    this.setState({ animating: false });

  49.                    console.error(error);

  50.                } else {

  51.                    this.setState({ animating: false });

  52.                    console.log(url)

  53.                }

  54.            })

  55.        }

  56.    }

  57.    /*

  58.    * Handle Local File Method

  59.    * fileType Default == "" you can use it, to set the File Extension (pdf,doc,xls,ppt etc) when in the Url the File Extension is missing.

  60.    */

  61.    handlePressLocal = () => {

  62.        this.setState({ animating: true });

  63.        if (Platform.OS === 'ios') {

  64.            OpenFile.openDoc([{

  65.                url: SavePath + "demo.docx",

  66.                fileNameOptional: "test filename"

  67.            }], (error, url) => {

  68.                if (error) {

  69.                    this.setState({ animating: false });

  70.                } else {

  71.                    this.setState({ animating: false });

  72.                    console.log(url)

  73.                }

  74.            })

  75.        } else {

  76.            OpenFile.openDoc([{

  77.                url: SavePath + "demo.jpg",

  78.                fileName: "sample",

  79.                cache: false,

  80.                fileType: "jpg"

  81.            }], (error, url) => {

  82.                if (error) {

  83.                    this.setState({ animating: false });

  84.                } else {

  85.                    this.setState({ animating: false });

  86.                    console.log(url)

  87.                }

  88.            })

  89.        }

  90.    }

  91.    /*

  92.    * Binary in URL

  93.    * Binary String in Url

  94.    * fileType Default == "" you can use it, to set the File Extension (pdf,doc,xls,ppt etc) when in the Url you dont have an File Extensions

  95.    */

  96.    handlePressBinaryinUrl = () => {

  97.        this.setState({ animating: true });

  98.        if (Platform.OS === 'ios') {

  99.            OpenFile.openDocBinaryinUrl([{

  100.                url: "https://storage.googleapis.com/need-sure/example",

  101.                fileName: "sample",

  102.                fileType: "xml"

  103.            }], (error, url) => {

  104.                if (error) {

  105.                    console.error(error);

  106.                    this.setState({ animating: false });

  107.                } else {

  108.                    console.log(url)

  109.                    this.setState({ animating: false });

  110.                }

  111.            })

  112.        } else {

  113.            OpenFile.openDocBinaryinUrl([{

  114.                url: "https://storage.googleapis.com/need-sure/example",

  115.                fileName: "sample",

  116.                fileType: "xml",

  117.                cache: true

  118.            }], (error, url) => {

  119.                if (error) {

  120.                    console.error(error);

  121.                    this.setState({ animating: false });

  122.                } else {

  123.                    console.log(url)

  124.                    this.setState({ animating: false });

  125.                }

  126.            })

  127.        }

  128.    }

  129.    /*

  130.    * Base64String

  131.    * put only the base64 String without data:application/octet-stream;base64

  132.    * fileType Default == "" you can use it, to set the File Extension (pdf,doc,xls,ppt etc) when in the Url you dont have an File Extensions

  133.    */

  134.    handlePressb64 = () => {

  135.        this.setState({ animating: true });

  136.        if (Platform.OS === 'ios') {

  137.            OpenFile.openDocb64([{

  138.                base64: "",

  139.                fileName: "sample.png",

  140.                fileType: "png"

  141.            }], (error, url) => {

  142.                if (error) {

  143.                    console.error(error);

  144.                    this.setState({ animating: false });

  145.                } else {

  146.                    console.log(url)

  147.                    this.setState({ animating: false });

  148.                }

  149.            })

  150.        } else {

  151.            OpenFile.openDocb64([{

  152.                base64: "",

  153.                fileName: "sample",

  154.                fileType: "png",

  155.                cache: true

  156.            }], (error, url) => {

  157.                if (error) {

  158.                    console.error(error);

  159.                    this.setState({ animating: false });

  160.                } else {

  161.                    console.log(url)

  162.                    this.setState({ animating: false });

  163.                }

  164.            })

  165.        }

  166.    }

  167.    /*

  168.    * mp4 Video

  169.    */

  170.    handlePressVideo(video) {

  171.        this.setState({ animating: true });

  172.        if (Platform.OS === 'ios') {

  173.            OpenFile.playMovie(video, (error, url) => {

  174.                if (error) {

  175.                    console.error(error);

  176.                    this.setState({ animating: false });

  177.                } else {

  178.                    console.log(url)

  179.                    this.setState({ animating: false });

  180.                }

  181.            })

  182.        } else {

  183.            this.setState({ animating: false });

  184.            Alert.alert("Coming soon for Android")

  185.        }

  186.    }

  187.    setToggleTimeout() {

  188.        this.setTimeout(() => {

  189.            this.setState({ animating: !this.state.animating });

  190.            this.setToggleTimeout();

  191.        }, 2000);

  192.    }

  193.    render() {

  194.        return (

  195.            <View style={styles.container}>

  196.                <ActivityIndicator

  197.                    animating={this.state.animating}

  198.                    style={[styles.centering, { height: 80 }]}

  199.                    size="large" />

  200.                <Text style={styles.welcome}>

  201.                    Doc Viewer React Native

  202.        </Text>

  203.                <Button

  204.                    onPress={this.handlePress.bind(this)}

  205.                    title="打开远程文档"

  206.                    accessibilityLabel="See a Document"

  207.                />

  208.                <Button

  209.                    onPress={this.handlePressLocal.bind(this)}

  210.                    title="打开本地文档"

  211.                    accessibilityLabel="See a Document"

  212.                />

  213.                <Button

  214.                    onPress={this.handlePressBinaryinUrl.bind(this)}

  215.                    title="打开远程二进制文档"

  216.                    accessibilityLabel="See a Document"

  217.                />

  218.                <Button

  219.                    onPress={this.handlePressb64.bind(this)}

  220.                    title="打开Base64字符串"

  221.                    accessibilityLabel="See a Document"

  222.                />

  223.                <Button

  224.                    onPress={() => this.handlePressVideo("http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4")}

  225.                    title="打开视频"

  226.                    accessibilityLabel="See a Document"

  227.                />

  228.            </View>

  229.        );

  230.    }

  231. }

  232. const styles = StyleSheet.create({

  233.    container: {

  234.        flex: 1,

  235.        justifyContent: 'center',

  236.        alignItems: 'center',

  237.        backgroundColor: '#F5FCFF',

  238.    },

  239.    welcome: {

  240.        fontSize: 20,

  241.        textAlign: 'center',

  242.        margin: 10,

  243.    },

  244.    instructions: {

  245.        textAlign: 'center',

  246.        color: '#333333',

  247.        marginBottom: 5,

  248.    },

  249. });

截图

示例源码

GitHub - forrest23/ReactNativeComponents: React Native组件大全

微信不让跳转外链,可以点击查看原文来查看外链GitHub内容。

组件地址

GitHub - philipphecht/react-native-doc-viewer: React Native Doc Viewer (Supports file formats: xls,ppt,doc,xlsx,pptx,docx,png,jpg,pdf)

转载http://mp.weixin.qq.com/s/K2AGelC_jOHeCkIDQa0gqw

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值