1

Through post method in jquery I tried to download a pdf but it doesn't work. File is not getting downloaded. where am I doing wrong?

main.php

<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
        </head>
<body>
    <script>
        function downloadpdf(){
            alert("working");
        $.post('download.php',{pdf:'Intro.pdf'},function(data){if(data=="y"){alert("Downloaded");}});
    }
        </script>
    <button onclick="downloadpdf();">download</button>   
</body>
</html>

download.php

<?php
    if(isset($_POST['pdf'])){
    $bbpdf=$_POST['pdf'];
header("Content-disposition: attachment; filename=$bbpdf");
header("Content-type: application/pdf");
header('Content-Length: ' . filesize($bbpdf));
readfile($bbpdf);
echo "y";
}
Learning
  • 848
  • 1
  • 9
  • 32
  • What happens and what do you expect to happen? "doesn't work" is quite a weak problem description.. :P – Jite Mar 24 '15 at 06:23
  • File is not getting downloaded . I want to download a pdf file. – Learning Mar 24 '15 at 06:26
  • Any errors? What does the js console say? One thing I can see right away is that `echo "y";` will add a `y` to the end of the file, which I'm going to guess is not intended. You only want to print out the data from the file. – Jite Mar 24 '15 at 06:27
  • No errors. Actually I tried in download.php `if(isset($_POST['pdf'])){echo "y";}` it worked fine. So, problem with `header` to download a pdf – Learning Mar 24 '15 at 06:31

1 Answers1

3

Try this:

<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
    </head>
    <body>
        <a href="download.php?pdf=Intro.pdf">download</a>
    </body>
</html>

<?php
if(isset($_GET['pdf'])){
    $bbpdf = $_GET['pdf'];

    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false); // required for certain browsers
    header("Content-Type: application/pdf");
    header("Content-Disposition: attachment; filename=\"" . basename($bbpdf));
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . filesize($bbpdf));
    ob_clean();
    flush();
    readfile($bbpdf);
}

Note: Don't use AJAX. There is no cross-browser way to force the browser to show a save-as dialog in JavaScript for some arbitrary blob of data received from the server via AJAX.

prava
  • 3,916
  • 2
  • 24
  • 35
  • It works fine. So, Is there no way to download a file using the method which I posted? – Learning Mar 24 '15 at 06:33
  • You can use iframe. Check this link, if it can help you - http://stackoverflow.com/questions/3613526/php-file-download-using-post-data-via-jquery-ajax – prava Mar 24 '15 at 06:39
  • I have a problem with this. Loading icon keeps on displaying.file is getting dowloaded but loading icon doesn't hide though page is loaded fully.`$(window).on('beforeunload', function(event) { $('#loading').show(); }); $(window).on('load', function(event) { $('#loading').hide();});` – Learning Mar 24 '15 at 06:58
  • `beforeunload` triggered though page is not unloaded – Learning Mar 24 '15 at 06:59