Clicking headers and AJAX
I have a script that makes it download, and I am making a call to that through Javascript. However the dialog does not appear, 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 instead of showing the dialog down.
I know the JS variable msg contains the file with the correct headers, but I don't know what to do with it to display the download dialog.
Thanks everyone
ps Don't know where to put this JS or PHP stream.
EDIT:
I have the right approach. I'm sure of it:). A user comes to my site, they fill out a form and click the submit button. After a few seconds, their flyer should appear in a dialog box that they can download. For this:
I am making an AJAX call to get the file and download it. I am using PHP script to send headers. Now all I need is a way to open the dowload dialog!
source to share
It doesn't show a dialog for the very fact that its an Ajax call.
window.location.href = msg;
Something that redirects you. I don't think you need the ajax call, just call the page using the href link.
change
If you want to submit the form and show the upload dialog, please do as follows:
<script>
function showDialogBox(form) {
form.submit();
window.location.href = "/download.php?file=XXX&properFilename=XXX";
}
</script>
<form onsubmit="showDialogBox(this);">
</form>
source to share
My opinion is that this is to be expected, as sktrdie notes. I can't test right now, but if you think about it: The user is essentially not getting notified about the AJAX things you do. Suddenly, such a message will be annoying.
My guess is that if you create an iframe (even hidden?) On this page and redirect to that download url, you get this dialog. YMMV, HTH.
source to share
I haven't coded php for a while, so I'm not sure if there is something wrong with your code. But I think the problem is with your approach. You need to load download.php into frame not as response to ajax call. So basically you need to form an invisible iframe in the document and point it to download.php with get parameters appended to the url.
source to share