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

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.