1

I have a PHP script that downloads a PDF file from the server and prompts you to either open it, or save it. The script accepts a one time token, which is used in place of a file name, to hide the file name.

If you go to the actual php page, http://example.com/files/download/token the script works fine and it downloads the PDF.

I could just send people to that page with a standard link tag, but once the file downloads I need to update content on that page which is returned through that download script.

Is there any way to have ajax call open up a new window where the file will download and then return the data that I need to update the current page?

There is more to the download script, but the main piece is the actual downloading part:

 header("Content-type: application/pdf"); 
 $this->load->helper('file');
 readfile("static/temp_statements/".$local_file_name);
 unlink("static/temp_statements/".$local_file_name);
Bill
  • 5,478
  • 17
  • 62
  • 95
  • You need to donwload a file and after that, you need more data that will be used to update the page? – Ruan Mendes Feb 23 '13 at 20:07
  • Yea, so basically you click on the download link which contains a one time token to download file. Then once the file is downloaded I need to update that one time token link so they can click on it again. – Bill Feb 23 '13 at 20:10
  • So I want the ajax call to return the new token, so I can add that in, and also open up a new tab or window which will contain the actual download – Bill Feb 23 '13 at 20:10

1 Answers1

5

One trick that I have used in the past that might be useful to you is

  • Supply a query param in your ajax call to download the PDF. This will be a unique name.
  • The server process that streams the PDF for download sets a cookie with this unique name.
  • You poll in your page waiting for this cookie to appear.
  • When the cookie appears you can assume the file has downloaded, and you can do your contingent action.

And you do not need to open a window to make this happen. You could just append an invisible iframe like this:

$(some selector).append($("<iframe width='1' height='1' frameborder='0' src='" + url + "'></iframe>"));

In the interests of honesty and transparency, I originally found this idea from this SO answer and it worked for me: Detect when browser receives file download

Community
  • 1
  • 1
Mike Hogan
  • 9,933
  • 9
  • 41
  • 71