Welcome Guest, Not a member yet? Register   Sign In
Show ajax progress bar while uploading csv to db using jquery
#1

(This post was last modified: 06-12-2019, 07:21 AM by ciadmin.)

I have uploaded csv to db some calculations are performed while uploading so it take 20 to 30 sec depending on csv file now i want to add ajax progress bar to be appeared while csv is being uploading in db and showing uploading percentage.
index.php

Code:
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Upload Attandance CSV</title>
  </head>
<body>
 <form id="attandance" action="" method="post" enctype="multipart/form-data" class="form-horizontal">
                    <div class="form-group">
                     <label class="col-md-4 control-label">Choose CSV File</label>
                     <input type="file" name="file" id="file" accept=".csv" class="form-control " placeholder="File">
                     <input type="submit" name="upload" id="upload" value="Import" class="btn btn-primary  offset-5"> </div>
                      <div id="loading" class="text-center mtop20"></div>
                      </form><br>
                       <div class="alert alert-success" style="display: none;"></div>
  <script src="<?php echo base_url(); ?>vendor/jquery/jquery.min.js"></script>
  <script src="<?php echo base_url(); ?>assets/js/demo/datatables-demo.js"></script>
<script>
  $(function(){
  $('#attandance').on('submit',function(e){
         e.preventDefault();
         $.ajax({
                 url:"<?php echo base_url(); ?>time/addcsv",
               method:"POST",
               data:new FormData(this),
               contentType:false,
               cache:false,
               processData:false,
                beforeSend: function() {
               var i='<img width="70" src="<?php echo base_url(); ?>/images/loadingImage.gif" />';    
                $("#loading").html(i);
                },
               success:function(respense){
                $("#loading").hide();
                $('.alert-success').html('Attandance Uploaded successfully').fadeIn().delay(4000).fadeOut('slow');
           }, error: function(){
                alert('Could not add data');
              }
           });
       });
  });
</script>
</body>
</html>

Controller

Code:
[b] [/b]public function addcsv(){
  $this->load->model('time_m','m');
         $result=$this->m->csv();
            $msg['success']=false;
                $msg['type']='add';
               if ($result){
               $msg['success']=true;
               }
              echo json_encode($msg);
  }

Model

Code:
public function csv(){
      $filename=explode(".",$_FILES["file"]["name"]);
      if(end($filename)=="csv"){
       $handle=fopen($_FILES["file"]["tmp_name"],"r");
       $m=$n=$u=$v=$z=$i='';$dg=1;$test=1;$first=false;
       while($data=fgetcsv($handle)){
        if(!$first){
            $first=true;
        }else{
        $emp_id=$data[0];$date=$data[1];$status=$data[2];
         $emp_rec =$this->db->get_where('nexthon_employ', array('employee_id'=>$emp_id));
         $happ_rec = $emp_rec->row(); $eid=$happ_rec->id ;$expectedMin= $happ_rec->workingHours*60;   
        $d=DateTime::createFromFormat("d-m-y H:i",$date);
        $a=$d->format("Y-m-d");$b=$d->format("H:i"); $month=$d->format("M"); $year=$d->format("Y");
        $rec=$this->db->get_where('csv', array('emp_id'=>$emp_id,'actiondate'=>$a));
        if($rec->num_rows()>0)
            {   
                $t=$rec->row();
                if($status==0 && $i!=0){
         $m=$b;$u=$v=null;$z=$t->totlbreak;
         $DateTime = new DateTime($b);
            $c=strtotime($DateTime->format("H:i"));
            $k=strtotime("13:00");
            if($c<=$k){
             $DateTime->modify('+4 hours');
              $n=$DateTime->format("H:i"); }
            else
              $n="17:00";
        }else if($status==1){
        $v=$t->breakOut;$z=$t->totlbreak;
         if($t->breakOut==$t->timeOut) 
         $v=$b; 
         $m=$t->timeIn;$u=$t->breakIn;$n=$b;
         if($i!=0){
            $DateTime = new DateTime($b);
            $c=strtotime($DateTime->format("H:i"));
            $k=strtotime("13:00");
            if($c>=$k){
             $DateTime->modify('-4 hours');
              $m=$DateTime->format("H:i");}
            else
              $m="09:00";
         $u=$v=null;$n=$b;
        } }
        else if($status==2 && $i==0){
        if($t->breakIn==null && $t->breakOut==null){ 
         $m=$t->timeIn;$u=$b;$v=$n=$t->timeOut;}
        else  if($t->breakIn!=null && $t->breakOut!=$t->timeOut){ 
         $m=$t->timeIn;$u=$b;$v=$n=$t->timeOut;
            $dteStart = new DateTime($t->breakIn); 
            $dteEnd   = new DateTime($t->breakOut); 
            $dteDiff  = $dteStart->diff($dteEnd); 
            $z=$dteDiff->format("%H:%I"); 
          if($t->totlbreak!=null){
            $time = $t->totlbreak;
            $time2 = $z;
            $secs = strtotime($time2)-strtotime("00:00");
            $z = date("H:i",strtotime($time)+$secs);
          }     }
        }else if($i==0 && $status==3 && $t->breakIn!=null){ $z=$t->totlbreak;
         $m=$t->timeIn;$v=$b;$u=$t->breakIn;
          $DateTime = new DateTime($b);
             $DateTime->modify('+1 minutes');
              $n=$DateTime->format("H:i");
        }
        $query=$this->db->query("UPDATE csv SET totlbreak='$z',timeIn='$m',timeOut='$n',breakIn='$u',breakOut='$v' WHERE `actiondate`='$a' AND `emp_id`='$emp_id'");
            }else{
     if($status==0){
         $m=$b;$u=$v=null;$i=0;
            $DateTime = new DateTime($b);
            $c=strtotime($DateTime->format("H:i"));
           $k=strtotime("13:00");
            if($c<=$k){
             $DateTime->modify('+4 hours');
              $n=$DateTime->format("H:i");}
            else
              $n="17:00";
          }
     else  if($status==1){
         $DateTime = new DateTime($b);
            $c=strtotime($DateTime->format("H:i"));
           $k=strtotime("13:00");
            if($c>=$k){
             $DateTime->modify('-4 hours');
              $m=$DateTime->format("H:i");}
            else
              $m="09:00";
         $u=$v=null;$n=$b;
        }
        $field=array(
        'eid'=>$eid,     
        'emp_id'=>$emp_id,
        'actiondate'=>$a,
        'type'=>'Working Day',
        'timeIn'=>$m,
        'timeOut'=>$n,
        'breakIn'=>$u,
        'breakOut'=>$v,
        'expectedMinutes'=>$expectedMin,
        'month'=>$month,
        'year'=>$year
        );
        $this->db->insert('csv',$field);$dg++;
            } }
        }fclose($handle);
        }
        if ( $this->db->affected_rows()>0) 
            return true;  
        else
            return false; 
     }
Reply


Messages In This Thread
Show ajax progress bar while uploading csv to db using jquery - by zeeshan - 06-12-2019, 03:11 AM



Theme © iAndrew 2016 - Forum software by © MyBB