【问题标题】:How do I use JS, JQuery to download or view a PDF after filling a form?填表后如何使用JS、JQuery下载或查看PDF?
【发布时间】:2019-09-20 15:26:39
【问题描述】:

我正在创建一个页面,用户可以在其中查看 PDF 文件,例如小册子。我希望他们先填写表格,将信息发送到电子邮件,然后页面重定向到打开 PDF。

由于我没有使用任何服务器端编码,我使用mailto:example@email.com 形式的post 部分来获取详细信息。

填表后如何使用JS、JQuery下载或查看PDF?

更新:我在玩,意识到使用mailto: 发布表单详细信息会打开计算机的电子邮件客户端,您必须通过该客户端手动单击发送。 这意味着可以在没有实际收到下载者详细信息的情况下查看 PDF。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet" />

<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<!-- Button trigger modal -->
<button type="button" class="btn btn-info btn-lg btn-block" data-toggle="modal" data-target="#myModal2">
  Get Brochure
</button>

<!-- Modal -->
<div class="modal right fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModal2">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
							aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel2">View Sample Brochure</h4>
      </div>
      <div class="modal-body">
        <p>Please fill out the form below first</p>
        <form method="post" action="" enctype="text/plain">
          <div class="form-group">
            <input type="text" class="form-control" id="firstName" Name="First Name" placeholder="First Name" required>
          </div>
          <div class="form-group">
            <input type="text" class="form-control" id="lastName" Name="Last Name" placeholder="Last Name" required>
          </div>
          <div class="form-group">
            <input type="text" class="form-control" id="Company" Name="Company" placeholder="Company" required>
          </div>
          <div class="form-group">
            <input type="email" class="form-control" id="usermail" Name="MailId" placeholder="E-mail Address" required>
          </div>
          <button type="submit" class="btn btn-default btn-lg btn-block">Submit</button>
        </form>
      </div>

    </div>
    <!-- modal-content -->
  </div>
  <!-- modal-dialog -->
</div>
<!-- modal -->

【问题讨论】:

  • 你可以给我的download()函数传递一个url,它会触发客户端下载,只要你能用ajax到达这个url。 github.com/rndme/download
  • 您需要使用jspdf.js 来查看或下载pdf。 jspdf.js 可在github.com/MrRio/jsPDF 获得
  • jspdf.js 看起来很棒。是否有关于如何链接 html 表单帖子以发送表单输入详细信息然后重定向到打开的 PDF 的示例?

标签: javascript jquery html forms


【解决方案1】:

如果 PDF 位于已知 url,您可以自动单击链接以查看或下载它:

function download (href) {
  const anchor = document.createElement('a');
  anchor.href = href;
  anchor.download = true;
  document.body.appendChild(anchor);
  anchor.click();
}

download('https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf');

【讨论】:

  • 如果我使用&lt;a href=""&gt;,如何获取要发布的表单?
  • 两个不同的东西。你会先张贴表格。
猜你喜欢
  • 2016-07-23
  • 2019-07-05
  • 1970-01-01
  • 2016-04-13
  • 2022-07-21
  • 2015-09-27
  • 1970-01-01
  • 2012-03-12
  • 2013-10-09
相关资源
最近更新 更多