摘要:文档查看组件,可以在手机上直接打开文档,支持远程和本地文档。
本文原创首发于公众号:ReactNative开发圈,转载需注明出处。
</>复制代码
React Native 文档查看组件:react-native-doc-viewer,可以在手机上直接打开文档,支持远程和本地文档。支持的文档格式:xls,ppt,doc,xlsx,pptx,docx,png,jpg,pdf,mp4。支持iOS和Android。
安装方法
</>复制代码
npm install react-native-doc-viewer --save
react-native link react-native-doc-viewer
API说明
openDoc 打开远程货或本地文档
openDocb64 打开Base64字符串格式文档
openDocBinaryinUrl 打开二进制文件
playMovie 打开视频文件
使用示例</>复制代码
import React, { Component } from "react";
import {
AppRegistry,
StyleSheet,
Text,
View,
Platform,
Button,
Alert,
ActivityIndicator
} from "react-native";
import OpenFile from "react-native-doc-viewer";
var RNFS = require("react-native-fs");
var SavePath = Platform.OS === "ios" ? RNFS.MainBundlePath : RNFS.DocumentDirectoryPath;
export default class Component02 extends Component {
static navigationOptions = ({ navigation }) => ({
title: `${navigation.state.params.name}`,
});
state = { animating: false }
/*
* Handle WWW File Method
* 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.
*/
handlePress = () => {
this.setState({ animating: true });
if (Platform.OS === "ios") {
OpenFile.openDoc([{
url: "https://calibre-ebook.com/downloads/demos/demo.docx",
fileNameOptional: "test filename"
}], (error, url) => {
if (error) {
this.setState({ animating: false });
} else {
this.setState({ animating: false });
console.log(url)
}
})
} else {
//Android
this.setState({ animating: true });
OpenFile.openDoc([{
url: "https://www.huf-haus.com/fileadmin/Bilder/Header/ART_3/Header_HUF_Haus_ART_3___1_.jpg", // Local "file://" + filepath
fileName: "sample",
cache: false,
fileType: "jpg"
}], (error, url) => {
if (error) {
this.setState({ animating: false });
console.error(error);
} else {
this.setState({ animating: false });
console.log(url)
}
})
}
}
/*
* Handle Local File Method
* 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.
*/
handlePressLocal = () => {
this.setState({ animating: true });
if (Platform.OS === "ios") {
OpenFile.openDoc([{
url: SavePath + "demo.docx",
fileNameOptional: "test filename"
}], (error, url) => {
if (error) {
this.setState({ animating: false });
} else {
this.setState({ animating: false });
console.log(url)
}
})
} else {
OpenFile.openDoc([{
url: SavePath + "demo.jpg",
fileName: "sample",
cache: false,
fileType: "jpg"
}], (error, url) => {
if (error) {
this.setState({ animating: false });
} else {
this.setState({ animating: false });
console.log(url)
}
})
}
}
/*
* Binary in URL
* Binary String in Url
* 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
*/
handlePressBinaryinUrl = () => {
this.setState({ animating: true });
if (Platform.OS === "ios") {
OpenFile.openDocBinaryinUrl([{
url: "https://storage.googleapis.com/need-sure/example",
fileName: "sample",
fileType: "xml"
}], (error, url) => {
if (error) {
console.error(error);
this.setState({ animating: false });
} else {
console.log(url)
this.setState({ animating: false });
}
})
} else {
OpenFile.openDocBinaryinUrl([{
url: "https://storage.googleapis.com/need-sure/example",
fileName: "sample",
fileType: "xml",
cache: true
}], (error, url) => {
if (error) {
console.error(error);
this.setState({ animating: false });
} else {
console.log(url)
this.setState({ animating: false });
}
})
}
}
/*
* Base64String
* put only the base64 String without data:application/octet-stream;base64
* 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
*/
handlePressb64 = () => {
this.setState({ animating: true });
if (Platform.OS === "ios") {
OpenFile.openDocb64([{
base64: "",
fileName: "sample.png",
fileType: "png"
}], (error, url) => {
if (error) {
console.error(error);
this.setState({ animating: false });
} else {
console.log(url)
this.setState({ animating: false });
}
})
} else {
OpenFile.openDocb64([{
base64: "",
fileName: "sample",
fileType: "png",
cache: true
}], (error, url) => {
if (error) {
console.error(error);
this.setState({ animating: false });
} else {
console.log(url)
this.setState({ animating: false });
}
})
}
}
/*
* mp4 Video
*/
handlePressVideo(video) {
this.setState({ animating: true });
if (Platform.OS === "ios") {
OpenFile.playMovie(video, (error, url) => {
if (error) {
console.error(error);
this.setState({ animating: false });
} else {
console.log(url)
this.setState({ animating: false });
}
})
} else {
this.setState({ animating: false });
Alert.alert("Coming soon for Android")
}
}
setToggleTimeout() {
this.setTimeout(() => {
this.setState({ animating: !this.state.animating });
this.setToggleTimeout();
}, 2000);
}
render() {
return (
Doc Viewer React Native
title="打开视频"
accessibilityLabel="See a Document"
/>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#F5FCFF",
},
welcome: {
fontSize: 20,
textAlign: "center",
margin: 10,
},
instructions: {
textAlign: "center",
color: "#333333",
marginBottom: 5,
},
});
Screenshots
示例源码
GitHub - forrest23/ReactNativeComponents: React Native组件大全
组件地址GitHub - philipphecht/react-native-doc-viewer: React Native Doc Viewer (Supports file formats: xls,ppt,doc,xlsx,pptx,docx,png,jpg,pdf)
举手之劳关注我的微信公众号:ReactNative开发圈
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/92045.html
摘要:写在前面一个好的缺不了好的三方支持,生活在这个活跃的开源社区,寻找合适的三方组件是一个开发者最基本的能力。下面分享几个我收集的三方模块,希望对大家有点帮助。 写在前面 一个好的App缺不了好的三方支持,生活在ReactNative这个活跃的开源社区,寻找合适的三方组件是一个开发者最基本的能力。不过不积跬步,无以至千里,不积小流,无以成江海。下面分享几个我收集的三方模块,希望对大家有点帮...
摘要:如果你想减少包大小,你可以这样引入事实上,每个组件都是支持单独安装的,我们也推荐你使用这种方式引入组件。以下是运行示例后各界面的截图组件图标右上角的圆形徽标数字。 1. 前言 一直以来都想做个组件库,一方面是对工作中常遇问题的总结,另一方面也确实能够提升工作效率(谁又不想造一个属于自己的轮子呢~),于是乎就有了本文的主角儿rn-components-kit。 市面上web的UI组件库如...
摘要:组件开发利器对于前端开发来说,组件化技术已经是一门必修课了,这其中又主要以和为主。 react、vue 组件开发利器:storybook 对于前端开发来说,组件化技术已经是一门必修课了,这其中又主要以 react 和 vue 为主。但平时在开发组件,尤其是公共组件或者第三方组件库的时候,往往会有一些困扰: 不能很好的管理多个组件,尤其是在组件预览的时候,不能一目了然 在组件预览的时候...
摘要:数据可视化库超过的的可能是最流行和最广泛的数据可视化库。是一组组件,用于高效地渲染大型列表和表格数据。一种优雅而灵活的方式,可以利用组件来支持实际的数据可视化。 想阅读更多优质文章请猛戳GitHub博客,一年百来篇优质文章等着你! React Native 组件库 1. NativeBase showImg(https://segmentfault.com/img/bVbrLHH?w=...
阅读 3229·2021-09-22 15:20
阅读 2736·2019-08-30 15:54
阅读 2108·2019-08-30 14:06
阅读 3303·2019-08-30 13:05
阅读 2629·2019-08-29 18:36
阅读 703·2019-08-29 15:10
阅读 647·2019-08-29 11:17
阅读 960·2019-08-28 18:11