【问题标题】:React pdf onclick downloadReact pdf onclick 下载
【发布时间】:2018-06-21 08:58:10
【问题描述】:

我的反应有问题。我有一个嵌入 pdf 和下载按钮的程序。我想在点击按钮时下载 pdf。

相反,我的程序将我重定向到您下载 pdf 的另一个页面,这意味着它存在于应用程序中,但我只想留在同一个应用程序中并在我的应用程序中下载 pdf。

有什么插件可以用来做这个吗?

请在下面找到我的代码:

<div>
    <object data={this.props.file} type='application/pdf'>
        <embed src={this.props.file} type='application/pdf' />
    </object>
    {
     this.props.author ==='bot' ?
        <a href={this.props.file} download={this.props.file}>
            <input alt='download' type={'image'} src={download} />
        </a>
     :
        ''
    }
 </div>

【问题讨论】:

    标签: javascript html reactjs pdf


    【解决方案1】:

    您可以使用基本的 HTML 来处理这个问题。你不需要任何插件。我更喜欢使用 reactstrap 而不是 bootstrap。

    import { Row, Col } from "reactstrap";
               <Row>
                {/* off set will middle the col */}
                <Col md={{ size: 8, offset: 2 }}>
                  <div >
                    <a
                      //this will save the file as "your_cv.pdf"
                      download="your_cv.pdf"
                      //put the path of your pdf file
                      href="/static/resume.pdf"
                      //reactstrap classes. add green button
                      className="btn btn-success"
                    >
                      Download
                    </a>
                  </div>
                  <iframe
                    style={{ width: "100%", height: "800px" }}
                    src="/static/resume.pdf"
                  >
                    {" "}
                  </iframe>
                </Col>
              </Row>
    

    【讨论】:

      【解决方案2】:

      您可以将 pdf 文件下载为 blob,并临时创建一个链接以将该 blob 下载为文件。您可以执行以下操作:

      fetch(this.props.file, {
          method: 'GET',
          headers: {...headers},
      })
        .then(res => res.blob())
        .then(blob => {
          const url = window.URL.createObjectURL(new Blob([blob]));
          const link = document.createElement('a');
          link.href = url;
          link.setAttribute('download', 'YOUR_PDF_NAME.pdf');
          document.body.appendChild(link);
          link.click();
          link.parentNode.removeChild(link);
        });
      

      不确定您的pdf托管服务器是如何配置的,您可以相应地设置请求的标头。

      【讨论】:

        【解决方案3】:

        使用 fetch 调用尝试以下方法并使用 FileReader 对象保存文件

        fetch(this.props.file, {
            method: "GET",
            headers: {
              Accept: "application/pdf",
              "Content-Type": "application/pdf",
            },
        
          }).then(response => response.blob())
            .then(response => {
          var blob=response
          var reader = new window.FileReader();
          reader.readAsDataURL(blob);
          reader.onloadend = function() {
          var base64data = reader.result;
        
              window.open(base64data);
        
          }
        })
        .catch(error => {
          console.error(error);
        });
        

        【讨论】:

        • 问题是我不想去另一个页面我不想留在我的应用程序中并且点击一个按钮必须在应用程序内下载pdf而不打开新标签
        猜你喜欢
        • 2022-11-30
        • 1970-01-01
        • 2020-08-07
        • 1970-01-01
        • 2022-06-16
        • 2014-12-28
        • 1970-01-01
        • 2021-03-02
        • 1970-01-01
        相关资源
        最近更新 更多