i am creating a website for a client using codeigniter which will be used to manage study resources in pdf format. What i want is when a user downloads the pdf, the site measure the download progress and then on download complete, it flags in the db that they have successfully downloaded the file then will not allow them to download it a second time.
My problem is on measuring the download progress. here is my code
public function download($id)
{
if($this->ion_auth->logged_in())
{
$user_id = $this->ion_auth->user()->row()->id;
if($user_id != $id){
show_error('We could not find any payment record for this resource. Be sure you have paid or contact us for any queries.');
}
// check resource using otp and resource id
$resource_id = $this->input->get('resource_id', true);
$otp = $this->input->get('otp', true);
$is_valid = $this->downloads_m->validate_resource($resource_id, $otp);
if(!count($is_valid))
{
show_error('The OTP and Resource you have requested do not match our records');
}
if(!$is_valid->pending)
{
show_error('Our records are showing that you have not paid for this resource. Please contact us if you have any query pertaining to this issue');
}
// get file here
$resource = $this->resources_m->get($resource_id, true);
$resource_folder = date('Y_m', strftime(strtotime($resource->created)));
$this->load->helper('download');
force_download('./assets/study_resources/' . $resource_folder . '/' . $resource->slug . '.pdf', null);
}
}
js
$('.btn-download').on('click', function(){
var $this = $(this);
var url = $this.attr('href');
$.ajax({
url: url,
type: 'POST',
ajaxStart: $this.html('Loading...'),
success: function(d){
console.log(d);
$this.html('<i class="fa fa-download"></i>');
}
});
return false;
});
in the dev console, i am only getting a network response which a long string, and but it is not even starting the download. Any suggestions?