views:

931

answers:

4

Hello all,

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!!

+2  A: 

It's not showing the dialog box for the very fact that its an Ajax call.

window.location.href = msg;

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:

<script>
function showDialogBox(form) {
    form.submit();
    window.location.href = "/download.php?file=XXX&properFilename=XXX";
}
</script>
<form onsubmit="showDialogBox(this);">

</form>
Luca Matteis
I do need ajax, I need it show the download dialog box without reloading page.
Abs
just creating a link, if the headers are set correctly, wont reload a page.... <a href="/download.php">link</a>
Luca Matteis
Do I put it as a string?
Abs
Check my edit, hope it helps.
Luca Matteis
Thanks, its not exactly what I wanted but it works i guess. I just it was automatic without user clicking a button.
Abs
A: 

If it full download.php script I can't find in it variable $file (only a $filename) - but in JS you send a $file variable. Second: something wrong in JS - why you use such variable name msg in data: and in success:?

Sergey Kuznetsov
I changed the name of that variable, it wasn't causing any problems, just bad programming skills.
Abs
A: 

My take is that this may be expected, as sktrdie notes. I can't test right now, but if you think about it: the user, per se, doesn't get any notification about AJAX stuff you're doing. Having suddenly such a message would be irritating.

I guess that if you create a (even hidden?) iframe on that page and redirect to that download URL that you would get that dialog. YMMV, HTH.

mark
Its not a message, its a file they have requested.
Abs
I read it this. Was my answer not clear in that case?
mark
A: 

I haven't coded php for a while so I'm not sure if there's anything wrong with your code. But I think the problem is in your approach. You need to load download.php in a frame not as a response to an ajax call. So basically you need to form an invisible iframe in the document and point it to download.php whith the get parameters appended to the url.

Vasil