【问题标题】:How can I add existing pdf file to my React.js page?如何将现有的 pdf 文件添加到我的 React.js 页面?
【发布时间】:2019-12-28 16:39:40
【问题描述】:

有人可以向我解释我应该如何将现有的 pdf 文件添加到我的 react 网页吗?

【问题讨论】:

    标签: javascript reactjs pdf


    【解决方案1】:

    我开发了这个组件用于我的项目

    import React, { Component } from "react";
    import Axios from 'axios';
    
    
    class DPFReportViewer extends Component {
        _isMounted = false;
    
        constructor(props) {
            super(props);
            this.state = {
                PdfURL: ""
            };
    
        }
    
        componentWillUnmount() {
            this._isMounted = false;
        }
    
        componentDidMount() {
            this._isMounted = true;
            this.setState({ PdfURL: '' });        
    
            Axios.post(
                '/Report/RunReport/',{ FileName: this.props.repFileName }, { 
                    responseType: 'arraybuffer',
                    headers: {
                         'Accept': 'application/pdf'
                    }
            ).then((response) => {
                if (this._isMounted && response.data) {
                    let blob = new Blob([response.data], { type: 'application/pdf' })
                    let fileURL = window.URL.createObjectURL(blob);                    
                    this.setState({ PdfURL: fileURL });
             }
            }).catch(error => {         
                console.log(error);
            })
        }
    
        render() {
            return (<div>
                <embed src={this.state.PdfURL} width="100%" height="500px" type="application/pdf" style={{ overflow: "auto", width: "100%", height: "500px", border: "1px groove  rgba(0, 0, 0, 0.5)" }} />
            </div>
            )
        }
    };
    
    export default DPFReportViewer;
    

    【讨论】:

    • 我刚刚用 添加了它,它工作正常...阿尔多我不知道它会如何工作在构建模式中...
    猜你喜欢
    • 2014-06-28
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多