【问题标题】:Way to display uploaded file in react native Expo在 React Native Expo 中显示上传文件的方式
【发布时间】:2021-03-15 10:26:29
【问题描述】:

有没有办法在 react native 中显示或预览上传的文件(png、doc、pdf)? 我已经编写了要上传的代码并且它正在工作,但我想显示该文件。有人知道吗? 我正在使用 Expo 和 TypeScript 的原生反应

【问题讨论】:

    标签: typescript react-native expo


    【解决方案1】:

    尝试使用react-native-pdf 存储库

    import React from 'react';
    import { StyleSheet, Dimensions, View } from 'react-native';
    
    import Pdf from 'react-native-pdf';
    
    export default class PDFExample extends React.Component {
        render() {
            const source = {uri:'http://samples.leanpub.com/thereactnativebook-sample.pdf',cache:true};
            //const source = require('./test.pdf');  // ios only
            //const source = {uri:'bundle-assets://test.pdf'};
    
            //const source = {uri:'file:///sdcard/test.pdf'};
            //const source = {uri:"data:application/pdf;base64,JVBERi0xLjcKJc..."};
    
            return (
                <View style={styles.container}>
                    <Pdf
                        source={source}
                        onLoadComplete={(numberOfPages,filePath)=>{
                            console.log(`number of pages: ${numberOfPages}`);
                        }}
                        onPageChanged={(page,numberOfPages)=>{
                            console.log(`current page: ${page}`);
                        }}
                        onError={(error)=>{
                            console.log(error);
                        }}
                        onPressLink={(uri)=>{
                            console.log(`Link presse: ${uri}`)
                        }}
                        style={styles.pdf}/>
                </View>
            )
      }
    }
    
    const styles = StyleSheet.create({
        container: {
            flex: 1,
            justifyContent: 'flex-start',
            alignItems: 'center',
            marginTop: 25,
        },
        pdf: {
            flex:1,
            width:Dimensions.get('window').width,
            height:Dimensions.get('window').height,
        }
    });
    

    【讨论】:

    • 只用于pdf吗?
    • @royxd 是的,仅适用于 pdf
    猜你喜欢
    • 2022-06-21
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    相关资源
    最近更新 更多