I have a script that forces a download and I make a call to this via Javascript. However, the dialog box doesn’t pop up, here is the download.php script:
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: $ctype'); // change, added quotes to allow spaces in filenames, by Rajkumar Singh header('Content-Disposition: attachment; filename=\''.basename($properFilename).'\';' ); header('Content-Transfer-Encoding: binary'); header('Content-Length: '.filesize($filename)); readfile('$filename'); exit();
Here is the Javascript (using JQuery):
///force download $.ajax({ type: 'GET', url: 'download.php', data: 'file=' + msg + '&properFilename=' + properFileName, success: function(msg){ window.location.href = msg; });//ajax
This redirects my browser to another page rather than showing the down dialog box.
I know the JS variable msg contains the file with the right headers but I don’t know what to do with it to get it to display the download dialog box.
Thanks all
p.s. Didn’t know where to put this thread JS or PHP.
EDIT:
I have the right approach I am sure of that 🙂 – A user comes to my site, they fill in a form and they press submit. After a few seconds their fle should show up in a dialog box that they can download. To do this:
I make an AJAX call to get the file and download it. I use the PHP script to send the headers. Now all I need is a way to get the dowload dialog box to show up!!
It’s not showing the dialog box for the very fact that its an Ajax call.
Thats what’s redirecting you. I don’t think you need an ajax call here, just call the page normally with an href link.
edit
If you want the form to submit and show the dialog box for the download, do this: