首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改事件时的iframe pdf预览

更改事件时的iframe pdf预览
EN

Stack Overflow用户
提问于 2021-01-28 02:48:38
回答 2查看 175关注 0票数 0
代码语言:javascript
复制
$(document).ready(function(){

  $("#id_quotationFile").change(function(){

    url=$("#id_quotationFile").attr(value);
    $("#pdf-view").attr("src",url);
    
  });
});

尝试使用上面的jquery代码加载在输入字段中浏览的pdf文件,但不起作用

代码语言:javascript
复制
<input type=file id="id_quotationFile">
           

<iframe src="" id="pdf-view" frameborder="0px" title="Preview"></iframe>

如何在iframe中预览pdf文件。

EN

回答 2

Stack Overflow用户

发布于 2021-01-28 03:29:07

正如这个问题中提到的,尝试强制您的iframe重新加载:

document.getElementById('some_frame_id').contentWindow.location.reload();

What’s the best way to reload / refresh an iframe?

在您的情况下,它将是:

代码语言:javascript
复制
$("#pdf-view").contentWindow.location.reload();
票数 0
EN

Stack Overflow用户

发布于 2021-01-28 03:42:37

请考虑以下内容。

代码语言:javascript
复制
$(function() {
  function readURL(input, target) {
    input = $(input).get(0);
    if (input.files && input.files[0]) {
      var reader = new FileReader();
      reader.onload = function(e) {
        $(target).attr('src', e.target.result);
      }
      reader.readAsDataURL(input.files[0]);
      $(".details").html("Preview: '" + input.files[0].name + "' Type: " + input.files[0].type);
    }
  }

  $("#fileUpload").click(function() {
    $("#id_quotationFile").trigger("click");
  });
  $("#id_quotationFile").change(function() {
    readURL(this, $("#pdf-view"));
  });
});
代码语言:javascript
复制
input {
  display: none;
}

button {
  padding: 0.2em 0.4em;
  margin: 0.2em;
}

iframe {
  width: 95%;
  height: 840px;
}

.details {
font-family: Arial, Monospace;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="fileUpload">Browse...</button>
<input type=file id="id_quotationFile">
<span class="details">&nbsp;</span>
<iframe src="" id="pdf-view" frameborder="0px" title="Preview"></iframe>

参考:Preview an image before it is uploaded

您需要将本地文件转换为新的FileReader。因此,我们读取文件,将其转换为Base64,当它准备就绪时,将其加载到目标中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65925506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档