Is it possible to use WordPress Ajax to download files. I have this function to download the attachment.
function download_attachment()
{
$file_path = $_POST['filename'];
$file_mime = $_POST['mime'];
$data['file_path'] = file_exists($file_path);
try{
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_path)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$file_mime);
header('Content-Disposition: attachment; filename="'.basename($file_path).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file_path)); // provide file size
header('Connection: close');
set_time_limit(0);
@readfile("$file_path") or die("File not found.");
}catch(Exception $e)
{
$data['error'] = $e->getMessage() ." @ ". $e->getFile() .' - '. $e->getLine();
}
}
echo json_encode($data);
die();
}
It is hooked to WordPress main function with this function:
add_action('wp_ajax_download_attachment','download_attachment');
And the jQuery code is this:
var data = {
'function': 'download_attachment',
'filename': file_path,
'mime': mime
};
jQuery.ajax({
url: ajaxurl,
type: "POST",
data: data,
success: function(return_data, textStatus, jqXHR) {
parsedData = kalimahJS.parseJSON(return_data);
window.open(parsedData.url);
}
})
The end result is 0 displayed on the screen. Is there another way to do this?