2

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?

drake035
  • 3,955
  • 41
  • 119
  • 229

1 Answers1

2

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'); 
Laz Karimov
  • 714
  • 10
  • 26
  • This is for a normal href link to PDF. In my case it's a button that leads to whatever and to which I have attached a PDF file using Javascript (but the original function/destination of the button remains intact) – drake035 Sep 02 '13 at 17:34
  • You mean you want to ask user from where to download it? – Laz Karimov Sep 02 '13 at 17:38
  • Well there's only 2 possibilities in Firefox: either a file is opened, or it is downloaded in which case the browser asks where to download it isn't it? – drake035 Sep 02 '13 at 17:40
  • First of all this script works in any browser. Opened where? In browser? May be I got what you meant, look at update – Laz Karimov Sep 02 '13 at 17:48
  • If you want to send some data and recieve it on Server side use `data: {},` line from example. On server side you can get data from $_POST – Laz Karimov Sep 02 '13 at 17:54