First off, I understand this is not the best way of forcing a file download, it's hacky and it's horrible, but hey, I just get told what to do!
So I have a WP installation, and I'm trying to leverage using the WP AJAX to download a file.
My form hits an AJAX callback in functions.php, and after doing some bits n pieces, I am hitting this function:
function download_file()
{
$file = get_template_directory()."/a_pdf_file.pdf";
if(file_exists($file)) {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
else
{
die('The provided file path is not valid.');
}
}
The problem is, the buffer is just being spat out in the AJAX response. No file is being downloaded.
Do I need to do something on the AJAX end, in the success callback maybe?
The other options I have tried seem to fail too.
header('Location: $file_url');
doesn't work- tried all manner of header options, still no difference
window.open(url, '_blank')
in AJAX callback gets blocked as a popupwindow.location()
opens in same window, not acceptable