I have a HTML page that allows user to download the file there's a button to download it, I'm using an onClick event to make an ajax call to a php script that gives the file data.
The problem is the file data is being recieved in the response but file is not dowloading.
I've already referred to this Php file download through ajax request post but is not solving my problem
HTML page
<button onClick="download()"> Download </button>
<script>
function download {
var url = "download"; //it is a code ignitor project hence it is just an action call.
var path ="<?php echo $data[0]->file_path . $data[0]->file_name; ?>";
$.post(url, {path: path}, function(result){
});
</script>
PHP script
public function download() {
$path = $this->input->post('path');
if(!file_exists($path)){
die('file not found');
} else {
header("Content-Disposition: attachment; filename=" . basename($path) . "");
header("Content-Length: " . filesize($path));
header("Content-Type: application/octet-stream;");
readfile($path);
}
}
Thanks.