I am using ajax to called download.php page , but it is not working .What could the problem in below code .
AJAX::
$.ajax({
type: "POST",
url: 'downloadsample.php',
data: {
imagepath: "../ReportCard/imagepost.png"
},
success:function(data) {
alert('sample download done ');
}
});
PHP Code ::
<?php
if ( isset($_POST["imagepath"]) && !empty($_POST["imagepath"]) ) {
$file = $_POST['imagepath'];
// Fetch the file info.
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
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.');
}
}
?>