drake035
drake035

Reputation: 2877

Downloading a PDF (not opening it) using jQuery

I need to trigger a file download using Javascript when user clicks on a certain button. To do that I use:

window.open('my_url.com/my_file.pdf');

However, in Firefox this opens the PDF in a new tab. I'd like instead that the PDF file doesn't get opened but downloaded, with a dialog box asking for a folder in which downloading the file.

How to do that?

Upvotes: 2

Views: 3087

Answers (1)

Laz  Karimov
Laz Karimov

Reputation: 714

Download jquery plugin from jqueryfiledownload.apphb.com

In JS

    $.fileDownload('pdfDownload.php', {
        httpMethod: 'POST',
        data: {},
        successCallback:function(){
        },
        failCallback:function(){
        }
    });     

In PHP
EDIT

In pdfDownload.php do whatever you want

// your PHP CODE
// ....

  header('Content-Type: application/pdf');
  header('Content-Disposition: attachment; filename=someName.pdf;');
  readfile('path/to/someName.pdf'); 

Upvotes: 2

Related Questions