0

Possible Duplicate:
Return automatic download JQuery Ajax Call

I am creating a file zip-fly with PHP. The file is created without problems. Even I have it set to not be saved on the server but immediately begin downloading.

In that file I call using jquery from another file by pressing a button. The code is as follows.

$(".down").on("click",function(){
  id = this.id;
  $.ajax({
    type: "POST",
    url: "files/down.php",
    data: "id="+id,
    cache: false,
    success: function(data){
        //alert(data)
    }
  });
});

I want that when you click on the button I display the browser download window and I do not do it ... and do not know how ...

Everything works correctly because if I tell PHP to save the file on the server does. If you tell me the answer in an alert() makes me full of strange symbols so something is returning, but not how to make it back to me with the download window.

Does anyone know how to do?

Community
  • 1
  • 1
  • I'd suggest simply submitting a form to a hidden iframe rather than using ajax for this. – Kevin B Jan 18 '13 at 19:47
  • @Blazemonger I have looked at the link you gave me and how to resolve it on that occasion by headers already had implemented in PHP and still not working. Thanks anyway. –  Jan 18 '13 at 20:10
  • @Kevin B I haven't clear what you mean by what you comment me... –  Jan 18 '13 at 20:11
  • basically, you would have a `
    ...
    ` and ``
    – Kevin B Jan 18 '13 at 20:12
  • @kevin B Finally I made ​​it work. Your system helped me. Thank you! –  Jan 19 '13 at 12:20
  • Regarding the issue duplicate, sorry. My English is not good and if you don't always know how to find the right information in your own language less in another... but try to be more attentive in next time. Anyway, the other issue and his response didn't help me. That code was already implemented and still not working. –  Jan 19 '13 at 12:25

1 Answers1

-1

If you dont want to use Iframe.

I used this method successfully:

  1. do the ajax call, and let the script zip it and save it temporary on the server.
  2. return link for this temporary stored file to your js / or link that will return a file on the fly.
  3. set the link to window.location.replace()

the code used by me.

On the js part just call the ajax request, and wait till the it finishes, This is what I used to return the file back to the script on server side for fake file location.:

$fakefile = $doc['doc_name'];

$finfo = new finfo(FILEINFO_MIME);
$mm_type = $finfo->file($realfile);

header("Cache-Control: public, must-revalidate"); 
header("Pragma: no-cache"); 
header("Content-Type: " . $mm_type); 
header("Content-Length: " .(string)(filesize($realfile)) ); 
header('Content-Disposition: attachment; filename="'.$fakefile.'"'); 
header("Content-Transfer-Encoding: binary"); 
readfile($realfile);
exit;

Back in js: catching the response:

$.post( href, null,
  function(data) {

  // file exist and is downloadable for user - call it for real.
  if(data.success === 1) {
     window.location.replace(href);
  }
  // there were errors  - display message.
  }
},'json');

Hope it helps.

Community
  • 1
  • 1
nakashu
  • 1,058
  • 12
  • 21