0

In the below code after the download I want to print the downloaded file through jquery/javascript.

But couldn't Identify where I need to put the print command and how need to process. any help would be appreciated - thanks

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
    $("a").click(function(e) {
        $('a#test').attr({target: '_blank', 
                href  : 'http://localhost/text.txt'});
    });
    var downloadURL = function downloadURL(url) {
        var hiddenIFrameID = 'hiddenDownloader',
            iframe = document.getElementById(hiddenIFrameID);
        if (iframe === null) {
            iframe = document.createElement('iframe');
            iframe.id = hiddenIFrameID;
            iframe.style.display = 'none';
            document.body.appendChild(iframe);
        }
        iframe.src = url;
    };
});
</script>
<body>
<a id="test" href="#">Download now!</a>
</body>
</html>
</head>
jcubic
  • 61,973
  • 54
  • 229
  • 402
Sridhar
  • 473
  • 3
  • 15
  • If you use jquery you can use `iframe = $('').hide().att('id', hiddenIFrameID).appendTo('body');` instead of `createElement` and `$('#hiddenDownloader')` instead of `getElementById`. – jcubic Aug 21 '13 at 07:20
  • it either the way we will be downloading to iframe, further how can I send to printer through jQuery. I can do the changes as suggested NP – Sridhar Aug 21 '13 at 07:23
  • You're not using downloadURL function, and after second click of the url you open new page with `text.txt`. – jcubic Aug 21 '13 at 07:23

1 Answers1

0

To print the content of the iframe just use:

iframe.contentWindow.print();
jcubic
  • 61,973
  • 54
  • 229
  • 402