简体   繁体   English

错误:无效或损坏的PDF文件Firefox

[英]Error: Invalid or corrupted PDF file Firefox

I am trying to upload a PDF file and once done uploading, I am trying to show that PDF file in an IFrame with the stream content I have with me in scope. 我正在尝试上传PDF文件,一旦完成上传,我将尝试在IFrame中显示该PDF文件,其中包含我在范围内拥有的流内容。 Same code works if I upload an image file but throws below error for PDF upload in Firefox. 如果我上传图像文件,则相同的代码有效,但在Firefox中上传PDF时出现以下错误。

Error 错误

Invalid or corrupted PDF file.
PDF.js v1.9.583 (build: d7b37ae7)
Message: Invalid PDF structure
viewer.js:1359:7
Error: Invalid or corrupted PDF file.

HTML 的HTML

<input type="file" id="files" name="files[]" multiple />

<iframe id="uploadedFileIframe" title="PDF in an i-Frame" src=""  scrolling="auto" height="400" width="400" />

CSS 的CSS

  .thumb {
    height: 75px;
    border: 1px solid #000;
    margin: 10px 5px 0 0;
  }

JavaScript 的JavaScript

function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {

      // Only process image files.
      if (!f.type.match('pdf.*')) {
        continue;
      }

      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
          // Render thumbnail.

          var enc = btoa(unescape(encodeURIComponent( e.target.result)))
          document.getElementById('uploadedFileIframe').setAttribute('src','data:application/pdf;base64,'+ enc);
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }
  }

  document.getElementById('files').addEventListener('change', handleFileSelect, false);

It looks like you've overengineered the solution a little bit 看来您对解决方案进行了过度设计

document.getElementById('uploadedFileIframe').src = reader.result;

should work if you place it instead of 如果您放置它而不是应该工作

var enc = btoa(unescape(encodeURIComponent( e.target.result)))   
document.getElementById('uploadedFileIframe').setAttribute('src','data:application/pdf;base64,'+ enc);

https://jsfiddle.net/kc8sdas5/ https://jsfiddle.net/kc8sdas5/

You are accepting multiple files to be selected and you are always replacing the same iframe with a new source. 您接受要选择的多个文件,并且始终使用新的源替换同一iframe。 You don't create a new iframe for each file. 您无需为每个文件创建新的iframe。 Also you don't have to read the file as a base64 string, it's pointless and also a bad for performances and memory. 另外,您不必将文件读取为base64字符串,它毫无意义,而且对性能和内存不利。 You could instead use the URL.createObjectURL instead, which is syncron, so you can get rid of your closure 您可以改用同步同步的URL.createObjectURL ,这样就可以消除闭包了

 function handleFileSelect(evt) { var files = evt.target.files; // FileList object var previews = document.querySelector('#previews'); var iframe; var URL = window.URL || webkitURL; // Loop through the FileList and render image files as thumbnails. for (var i = 0, f; f = files[i]; i++) { // Only process pdf files. if (!f.type.match('pdf.*')) { continue; } iframe = document.createElement('iframe') iframe.title = "PDF in an i-Frame"; iframe.scrolling = "auto"; iframe.width = iframe.hight = 400; iframe.src = URL.createObjectURL(f); previews.appendChild(iframe); } } document.getElementById('files').addEventListener('change', handleFileSelect, false); 
 <input type="file" id="files" name="files[]" multiple /> <div id="previews"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM