I have a simple PHP script on my the server, which should download a given file. It's working fine, if I invoke it directly with http://myDomain/download.php?filename=mini.gpx
download.php:
<?php
$dir = 'download/';
$file = $_GET['filename'];
$fqn = $dir . $file;
$fileSize = filesize($fqn);
header("Content-Type: text/xml");
header("Content-Disposition: attachment; filename=\"$file\"");
header("Content-Length: $fileSize");
readfile($fqn);
?>
But I want to start this script out of JavaScript. So I tried to do it with an httpRequest:
function downloadGPXfile(fn) {
let script = `downloadGPXfile.php?filename=${fn}`;
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
console.log("state downloadGPXfile: ", this.readyState);
console.log("status: ", this.status);
};
xhr.open('GET', script, true);
xhr.setRequestHeader('Content-Type', 'text/xml');
xhr.send();
}
Although the AJAX connection seems to be successful, the download dialog is not activated. What do I wrong? Or is there another, simplier solution to start the download?