2016-03-29 16 views
0

私のシステムにはテキストファイルがあります。私はそのテキストファイルの内容をjavascript/jqueryを使って表示したい。どうやってやるの。私はLinkを参照しました。しかし、単にdivから印刷するだけです。私はwindow.print()メソッドを使用する必要があります。しかし、どのように私はJavaScriptを使用してテキストファイルを印刷できますか?私を案内してください。javascript/jqueryを使用してテキストファイルの内容を印刷します

答えて

0

使用するコードは、あなたの問題を解決することがあります。 -

<html> 
<head> 
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js" > </script> 
<script type="text/javascript"> 

function PrintElem(event) 
{ 
    var selectedFile = event.target.files[0]; 
    var reader = new FileReader(); 
    var result = document.getElementById("mydiv"); 
    reader.onload = function(event) { 
     result.innerHTML = event.target.result; 
    }; 
    reader.readAsText(selectedFile); 
    console.log(selectedFile); 
    //Popup($('#mydiv').html()); 
} 
function Popup(elam) 
{ 
    //console.log($(elam).html());return false; 
    var data = $(elam).html(); 
    var mywindow = window.open('', 'my div', 'height=400,width=600'); 
    mywindow.document.write('<html><head><title>my div</title>'); 
    /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />'); 
    mywindow.document.write('</head><body >'); 
    mywindow.document.write(data); 
    mywindow.document.write('</body></html>'); 

    mywindow.document.close(); // necessary for IE >= 10 
    mywindow.focus(); // necessary for IE >= 10 

    mywindow.print(); 
    mywindow.close(); 

    return true; 
} 

</script> 
</head> 
<body> 

<div id="mydiv"> 

</div> 

<input type="file" onchange="PrintElem(event)"> 
<input type="button" value="Print Div" onclick="Popup('#mydiv')" /> 
</body> 
</html> 
1

まずオープンテキストファイルや印刷を。

var w = window.open('yourfile.txt'); //Required full file path. 
w.print(); 

フィドルサンプル:https://jsfiddle.net/shree/91459gm9/

フィドル印刷するファイルが、そのオープンサンプルファイルとプリントウィンドウを発見していません。私はあなたがコンピュータからファイルを読み込むためのバックエンド(PHP、レール、nodejs)のサポートが必要だと思うし、任意のHTMLブロックに表示

+0

おかげ@Shreeでなければなりません。私はそれがほとんどだと思います。私は試してみましょう – Santhucool

+0

フォントが正しく来ていない。私はいくつかの怪我をしているdフォント。テストプリントフォントと同じフォントが必要です。それをどうすれば実現できますか? – Santhucool

0

は、印刷これはあなたが使用できるファイル

を印刷する最も望ましい方法です。あなたは、バックエンドの設定とファイルを持っている場合は、構文の下に、サーバー上

$(ドキュメント).ready(関数(){

$("button").click(function() { 
     $.ajax({ 
      url : "Content.txt", 
      dataType: "text", 
      success : function (result) { 
       $("#container").html(result); 
      } 
     }); 
    }); 
}); 
関連する問題