0

I currently have a page on my server which contains links that allow me to download files from a specific folder on my server.

The links are made like :

<a href='file.php?file=$fileName'>FILE</a>

When I click on the link the file is read by file.php and then downloaded. file.php is similar to this :

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$file.'.zip');
header('Content-Length: ' . filesize($tmp));

ob_flush(); flush();
while(!feof($fd)) {
    $buffer = fread($fd, 2048);
    echo $buffer;
    ob_flush(); flush();
}

This is all working fine, but there is a slight delay as file.php compresses the file before it's downloaded.

I'm hoping to use Jquery to create an overlay to display 'please wait', run file.php and then close the overlay when the download starts.

$(".file").click(function(){
    var file = $(this).data("id");
    jQuery.colorbox({html:"<p>PLEASE WAIT</p>"});
    $.ajax({ 
        load: 'file.php?file=' + file 
    }); 
});

This shows the overlay but the file never downloads. Is there anyway I can call file.php and it run as if it was clicked normally so the file is downloaded?

Thanks

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Tom
  • 1,436
  • 24
  • 50
  • 1
    You can't download a file through AJAX. If you want to trigger the download automatically you would need to do a redirect, or trigger a click event on the relevant `a` element. – Rory McCrossan May 11 '16 at 10:00
  • Actually this is a better duplicate: [Download File Using Javascript/jQuery](http://stackoverflow.com/questions/3749231/download-file-using-javascript-jquery) – Liam May 11 '16 at 10:02
  • 1
    Send ajax request to your 'file.php' where it is giving back a response with a file link, then at ajax success function add your file link to a front-end and trigger a click on it – MakeLoveNotWar May 11 '16 at 10:02
  • @MancharyManchaary - do you have an example or link showing how to do this ? – Tom May 11 '16 at 11:33
  • idk why jsfiddle is not working for me – MakeLoveNotWar May 11 '16 at 12:24
  • `$.ajax({ url: "file.php", type: "POST", data: { 'file': file }, success: function(response) { //do something with a response; if (response.link_to_file) { $('a').attr('href', response.link_to_file); // OR //window.location.href = response.likn_to_file; } } });` and in your php script you must do `echo json_encode($response)` where `$response` hold the link to a file; `$response['link_to_file'] = $myFIle`... – MakeLoveNotWar May 11 '16 at 12:26
  • Thanks - I've partially got this working using one of the links above. `var url = "file.php?file=" + file; document.getElementById('file').src = url; $.colorbox.close();` but this closes the colorbox before the file starts downloading.. any ideas ? – Tom May 11 '16 at 13:00
  • 1
    start `colorbox` before ajax call, and at the `success:function()` part do `setTimout` since ajax does requests asynchroniusly, i think it just doing it too fast...or use `$.aja({...}).done(function(){ $.colorbox.close(); })` You can find more info here - http://api.jquery.com/jquery.ajax/ – MakeLoveNotWar May 11 '16 at 13:12

0 Answers0