2016-05-17 6 views
0

PDFを配列に読み込んで再配置しようとしています。私はちょうど "PDF文書を読み込めませんでした"というエラーを受け取ります。Javascript/Jquery BlobがChromeを表示していませんPDF

私はreadAsArrayBufferとreadAsBinaryStringを使ってみました。 ロックなしでUint8Arrayに変換しようとしました。

はまだ

Blob from javascript binary string

http://www.javascripture.com/Blob

https://developer.mozilla.org/en-US/docs/Web/API/FileReader

そして運を見てきました:私のコードは以下の通りです/

<!DOCTYPE html> 
    <html> 
     <head> 
      <script src="/jquery-1.7.1.min.js" type="text/javascript"></script> 
      <script>   
      var $j = jQuery; 
       function handleFileSelect() {    
       if (!window.File || !window.FileReader || !window.FileList || !window.Blob) { 
        alert('The File APIs are not fully supported in this browser.'); 
        return; 
       } 

       input = document.getElementById('fileinput'); 

       if (!input) { 
        alert("Um, couldn't find the fileinput element."); 
       } 
       else if (!input.files) { 
        alert("This browser doesn't seem to support the `files` property of file inputs."); 
       } 
       else if (!input.files[0]) { 
        alert("Please select a file before clicking 'Load'");    
       } 
       else { 
        file = input.files[0]; 
        fr = new FileReader(); 
        fr.onload = receivedText; 

        arrayBuffer = fr.readAsArrayBuffer(file); 

       } 
       } 

      var arrayBuffer; 

       function receivedText() { 
       document.getElementById('editor').appendChild(document.createTextNode(fr.result)); 
       }   

      function getFile(){ 
        var data = arrayBuffer; 
        var blob = new Blob([data], {type: "application/pdf"}); 

        var objectUrl = URL.createObjectURL(blob); 
        window.open(objectUrl); 
      } 
      </script> 
     </head> 
     <body> 
      <input type="file" id="fileinput"/> 
      <input type='button' id='btnLoad' value='Load' onclick='handleFileSelect();'> 
      <input type='button' value='Get' onclick='getFile();'> 
      <div id="editor"></div> 
     </body> 
    </html> 

答えて

1

だけ

<!DOCTYPE html> 
<html> 
    <head> 
     <script src="/jquery-1.7.1.min.js" type="text/javascript"></script> 
     <script> 
     var arrayBuffer="";  
     var $j = jQuery; 
      function handleFileSelect() {    
      if (!window.File || !window.FileReader || !window.FileList || !window.Blob) { 
       alert('The File APIs are not fully supported in this browser.'); 
       return; 
      } 

      input = document.getElementById('fileinput'); 

      if (!input) { 
       alert("Um, couldn't find the fileinput element."); 
      } 
      else if (!input.files) { 
       alert("This browser doesn't seem to support the `files` property of file inputs."); 
      } 
      else if (!input.files[0]) { 
       alert("Please select a file before clicking 'Load'");    
      } 
      else { 
       file = input.files[0]; 
       fr = new FileReader(); 
       fr.onload = receivedText; 
       arrayBuffer = fr.readAsBinaryString(file); 


      } 

      } 

     // 

      function receivedText() { 
      document.getElementById('editor').appendChild(document.createTextNode(fr.result)); 
      }   

     function getFile(){ 

       var data = file; 
       var blob = new Blob([data], {type: "application/pdf"}); 
       var objectUrl = URL.createObjectURL(blob); 
       window.open(objectUrl); 
     } 
     </script> 
    </head> 
    <body> 
     <input type="file" id="fileinput"/> 
     <input type='button' id='btnLoad' value='Load' onclick='handleFileSelect();'> 
     <input type='button' value='Get' onclick='getFile();'> 
     <div id="editor"></div> 
    </body> 
</html> 

編集したコードを使用してもただ詳細については、ヘルプ

<!DOCTYPE html> 
<html> 
    <head> 
     <script src="/jquery-1.7.1.min.js" type="text/javascript"></script> 
     <script> 
     var arrayBuffer="";  
     var $j = jQuery; 
      function handleFileSelect() {    
      if (!window.File || !window.FileReader || !window.FileList || !window.Blob) { 
       alert('The File APIs are not fully supported in this browser.'); 
       return; 
      } 

      input = document.getElementById('fileinput'); 

      if (!input) { 
       alert("Um, couldn't find the fileinput element."); 
      } 
      else if (!input.files) { 
       alert("This browser doesn't seem to support the `files` property of file inputs."); 
      } 
      else if (!input.files[0]) { 
       alert("Please select a file before clicking 'Load'");    
      } 
      else { 
       file = input.files[0]; 
       fr = new FileReader(); 
       fr.onload = receivedText; 
       arrayBuffer = fr.readAsDataURL(file); 

      } 

      } 

     // 

      function receivedText() { 
      document.getElementById('editor').appendChild(document.createTextNode(fr.result)); 
      }   

     function getFile(){ 
      //alert(arrayBuffer); 
       var data = document.getElementById('editor').innerHTML; 
       alert(data); 
       var blob = new Blob([data], {type: "application/pdf"}); 
       var objectUrl = URL.createObjectURL(blob); 
       window.open(data); 
     } 
     </script> 
    </head> 
    <body> 
     <input type="file" id="fileinput"/> 
     <input type='button' id='btnLoad' value='Load' onclick='handleFileSelect();'> 
     <input type='button' value='Get' onclick='getFile();'> 
     <div id="editor"></div> 
    </body> 
</html> 
+0

のための別のコードではない配列が訪問:http://www.7logic.info/2016/10/jquery-をpdf-file-as-array-and.htmlを読む – Akshath

関連する問題