0

I already found many questions here in SO about the theme, but any of them helps me with a specific problem.

I need to send a ajax request to PHP and then download a PDF file.

This is my Javascript

$('body').on("click",'.download-file', function(e) {
        e.stopPropagation();
        var id = $(this).attr('data-id');
            $.ajax({
                url: 'app/myPage/download',// The framework will redirect it to index.php and then to myPage.class and method dowload
                data: id,
                type: 'POST',
                success: function (return) {
                   console.log(return);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                   console.log("Error... " + textStatus + "        " + errorThrown);
                }
            });
    });

Here is my PHP file:

class myPage{
    public function download()
       {
            $id = $_POST['id'];
            $filePath = $this->methodToGetFile($id);
            $name = end(explode('/',$filePath));
            $fp = fopen($filePath, 'rb');
            header("Cache-Control: ");
            header("Pragma: ");
            header("Content-Type: application/octet-stream");
            header("Content-Length: " . filesize($filePath));
            header("Content-Disposition: attachment; filename='".$name."';");
            header("Content-Transfer-Encoding: binary\n");
            fpassthru($fp);
            exit;
    }
}

With this approach, I have the PDF "printed" in the console like

%PDF-1.5%����1 0 obj<</Type/Catalog/Pages 2 0 R/Lang(pt-BR) /StructTreeRoot 8 0 R/MarkInfo<</Marked true>>>> ....

So, I'd like to know what should I do to open this file and download it.

In the linked questions I see something about window.location, but I don't know how can I use it.

I've already tried change the PHP headers to try force the download, but no success with it. In these cases I just receive null in the javascript nothing happens. Here are the modified headers:

    header('Content-Type: application/pdf');//tried with this option
    //header('Content-Type: application/octet-stream');// also tried with this other option
    header('Content-Disposition: attachment; filename=' . $name);
    header('Content-Transfer-Encoding: binary');
    header('Connection: Keep-Alive');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($filePath));

Are there some good approach to do this?

Community
  • 1
  • 1
James
  • 1,653
  • 2
  • 31
  • 60
  • 1
    header('Content-Type: application/pdf'); instead of header('Content-Type: application/octet-stream'); – Ophitect Dec 16 '15 at 03:20
  • 1
    header('Content-type: application/pdf'); you have a small letter t? – Ophitect Dec 16 '15 at 03:25
  • I corrected the typo, but still nothing. :-(. Thanks anyway. – James Dec 16 '15 at 03:29
  • 1
    i don't think this approach to downloading a pdf will work. Javascript is sending the request, and php is sending the response. you want the response to go directly to the browser, not to javascript. Why not just have the link to download go directly to the download location, by pass ajax all together? – danjfoley Dec 16 '15 at 04:29
  • 1
    what i mean is something like.. onclick = window.href='app/myPage/download?'+id – danjfoley Dec 16 '15 at 04:31
  • @danjfoley. I'm trying to avoid the use of a GET request, because I need to send other information with the file ID, So a POST via AJAX looked like the best option. – James Dec 16 '15 at 12:19
  • 1
    then how about a hidden form post? – danjfoley Dec 16 '15 at 16:50
  • I'll try this one. thanks for your help – James Dec 16 '15 at 17:24
  • @danjfoley. I changed some things in my system and now I'm using a GET request. Thank you for your tip. If you can post your comment as a answer, I'll accept it to help anyone with similar problem. – James Jan 27 '16 at 23:41

1 Answers1

2

i don't think this approach to downloading a pdf will work. Javascript is sending the request, and php is sending the response. You want the response to go directly to the browser, not to javascript. You should change the download link to go directly to the download location. No ajax / javascript needed. Like this:

<a href="javascript:void(0)" onclick = "window.href='app/myPage/download?'+id">download</a>
danjfoley
  • 778
  • 1
  • 8
  • 20