-1

I use code from How to force file download with PHP

<?php

require('Service.php');
$service = new Service('config.json', getcwd() . '/..');
if ($service->valid_token($_GET['token']) && isset($_GET['filename'])) {
    header('Content-Type: application/octet-stream');
    header("Content-Transfer-Encoding: Binary");
    header("Content-disposition: attachment; filename=\"" . basename($_GET['filename']) . "\"");
    readfile($_GET['filename']); // do the double-download-dance (dirty but worky)
}

?>

and call it from javascript using jQuery get

var filename = cwd + '/' + cmd.args[0];
$.get('lib/download.php', {filename: filename, token: token}, function(response) {
    console.log(response);
});

but the file is not downloaded. I only get normal ajax response. How can I download a file using javascript? I need php script for this because I want to download php files or cgi scripts as well.

Community
  • 1
  • 1
jcubic
  • 61,973
  • 54
  • 229
  • 402

1 Answers1

0

You can't do this with ajax. You can open a form in a new window pointing to the script and have it close when the download is ready.

Or even use window.location with javascript to open the script in a new window.

Vasil Rashkov
  • 1,818
  • 15
  • 27