无法上传CSV文件

时间:2018-06-22 15:36:11

标签: php mysql ajax codeigniter

我正在尝试上传一个csv文件,这是Microsoft excel类型的文件,似乎在导入csv文件时出现问题。

控制器(Csv_import.php):

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 class Csv_import extends CI_Controller {

 public function __construct()
 {
  parent::__construct();
  $this->load->model('csv_import_model');
  $this->load->library('csvimport');
 }

  function index()
    {
      $this->load->view('csv_import');
      $this->load_data();
    }

  function load_data()
    {
      $result = $this->csv_import_model->select();
      $output = '
       <h3 align="center">Imported User Details from CSV File</h3>
       <div class="table-responsive">
         <table class="table table-bordered table-striped">
           <tr>
            <th>ID Number</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Gender</th>
            <th>Course</th>
            <th>Year</th>
          </tr>
              ';
      $count = 0;
      if($result->num_rows() > 0)
       {
        foreach($result->result() as $row)
         {
          $count = $count + 1;
          $output .= '
          <tr>
           <td>'.$count.'</td>
           <td>'.$row->fname.'</td>
           <td>'.$row->lname.'</td>
           <td>'.$row->gndr.'</td>
           <td>'.$row->crse.'</td>
           <td>'.$row->yr.'</td>
          </tr>
          ';
          }
         }
        else
         {
          $output .= '
           <tr>
           <td colspan="6" align="center">Data not Available</td>
           </tr>
          ';
    }
   $output .= '</table></div>';
   echo $output;
    }

   function import()
    {
     $file_data = $this->csvimport->get_array($_FILES["csv_file"] 
     ["tmp_name"]);
     foreach($file_data as $row)
      {
       $data[] = array(
      'id_number' => $row["ID Number"],
      'fname'  => $row["First Name"],
      'lname'  => $row["Last Name"],
      'gndr'   => $row["Gender"],
      'crse'   => $row["Course"],
      'yr'   => $row["Year"]
     );
    }
   $this->csv_import_model->insert($data);
  }


}

模型(csv_import_model.php):

<?php
 class Csv_import_model extends CI_Model
  {
   function select()
    {
      $this->db->order_by('id_number', 'DESC');
      $query = $this->db->get('tbl_user');
      return $query;
  }

   function insert($data)
    {
     $this->db->insert_batch('tbl_user', $data);
    }
  }

和视图(csv_import.php):

<html>
<head>
<title>CSV Sample</title>

<script type="text/javascript" src="<?php echo base_url();
>bootstrap/js/jquery.js" ></script>
<script type="text/javascript" src="<?php echo base_url();
>bootstrap/dist/js/bootstrap.js" ></script>
<link href="<?php echo base_url()?>bootstrap/dist/css/bootstrap.css" 
rel="stylesheet" type="text/css"/>

</head>
<body>
<div class="container box">
<h3 align="center">CSV Sample</h3>
<br />

<form method="post" id="import_csv" enctype="multipart/form-data">
 <div class="form-group">
  <label>Select CSV File</label>
  <input type="file" name="csv_file" id="csv_file" required accept=".csv" />
 </div>
 <br />
 <button type="submit" name="import_csv" class="btn btn-info" 
  id="import_csv_btn">Import CSV</button>
 </form>
 <br />
  <div id="imported_csv_data"></div>
  </div>
  </body>
  </html>
  <script>

$(document).ready(function(){

 load_data();

function load_data()
 {
  $.ajax({
  url:"<?php echo base_url(); ?>csv_import/load_data",
  method:"POST",
  success:function(data)
   {
    $('#imported_csv_data').html(data);
   }
   })
   }

 $('#import_csv').on('submit', function(event){
  event.preventDefault();
  $.ajax({
  url:"<?php echo base_url(); ?>csv_import/import",
  method:"POST",
  data:new FormData(this),
  contentType:false,
  cache:false,
  processData:false,
  beforeSend:function(){
 $('#import_csv_btn').html('Importing...');
  },
 success:function(data)
  {
   load_data();
   $('#import_csv')[0].reset();
   $('#import_csv_btn').attr('disabled', false);
   $('#import_csv_btn').html('Import Done');

    }
  })
 });

 });
</script>

当我单击导入按钮时,没有任何反应,在运行代码之前,我还下载了csv库。顺便说一下,我正在使用CodeIgniter

1 个答案:

答案 0 :(得分:0)

大声笑,我知道已经有一年了,但是我设法以某种方式设法解决了这个问题。我认为我自己的问题是,我从另一个来源复制了此代码,并保证此代码100%可用,因此为了使此代码起作用,我编辑了控制器,而不是这样:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 class Csv_import extends CI_Controller {

 public function __construct()
  {
  parent::__construct();
  $this->load->model('csv_import_model');
  $this->load->library('csvimport');
 }

  function index()
    {
      $this->load->view('csv_import');
      $this->load_data();
    }

  function load_data()
    {
      $result = $this->csv_import_model->select();
      $output = '
       <h3 align="center">Imported User Details from CSV File</h3>
       <div class="table-responsive">
       <table class="table table-bordered table-striped">
       <tr>
        <th>ID Number</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Gender</th>
        <th>Course</th>
        <th>Year</th>
      </tr>
          ';
      $count = 0;
      if($result->num_rows() > 0)
       {
        foreach($result->result() as $row)
         {
          $count = $count + 1;
          $output .= '
          <tr>
           <td>'.$count.'</td>
           <td>'.$row->fname.'</td>
           <td>'.$row->lname.'</td>
           <td>'.$row->gndr.'</td>
           <td>'.$row->crse.'</td>
           <td>'.$row->yr.'</td>
          </tr>
          ';
          }
         }
    else
     {
      $output .= '
       <tr>
       <td colspan="6" align="center">Data not Available</td>
       </tr>
      ';
    }
   $output .= '</table></div>';
   echo $output;
    }

   function import()
    {
     $file_data = $this->csvimport->get_array($_FILES["csv_file"] 
     ["tmp_name"]);
     foreach($file_data as $row)
       {
       $data[] = array(
      'id_number' => $row["ID Number"],
      'fname'  => $row["First Name"],
      'lname'  => $row["Last Name"],
      'gndr'   => $row["Gender"],
      'crse'   => $row["Course"],
      'yr'   => $row["Year"]
     );
    }
   $this->csv_import_model->insert($data);
  }


    }

我将$ output替换为echo,然后应该是这样:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 class Csv_import extends CI_Controller {

 public function __construct()
  {
  parent::__construct();
  $this->load->model('csv_import_model');
  $this->load->library('csvimport');
 }

  function index()
    {
      $this->load->view('csv_import');
      $this->load_data();
    }

  function load_data()
    {
      $result = $this->csv_import_model->select();
      $echo'
       <h3 align="center">Imported User Details from CSV File</h3>
       <div class="table-responsive">
       <table class="table table-bordered table-striped">
       <tr>
        <th>ID Number</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Gender</th>
        <th>Course</th>
        <th>Year</th>
      </tr>
          ';
      $count = 0;
      if($result->num_rows() > 0)
       {
        foreach($result->result() as $row)
         {
          $count = $count + 1;
          $echo '
          <tr>
           <td>'.$count.'</td>
           <td>'.$row->fname.'</td>
           <td>'.$row->lname.'</td>
           <td>'.$row->gndr.'</td>
           <td>'.$row->crse.'</td>
           <td>'.$row->yr.'</td>
          </tr>
          ';
          }
         }
    else
     {
      $echo'
       <tr>
       <td colspan="6" align="center">Data not Available</td>
       </tr>
      ';
    }
   $echo '</table></div>';
    }

   function import()
    {
     $file_data = $this->csvimport->get_array($_FILES["csv_file"] 
     ["tmp_name"]);
     foreach($file_data as $row)
       {
       $data[] = array(
      'id_number' => $row["ID Number"],
      'fname'  => $row["First Name"],
      'lname'  => $row["Last Name"],
      'gndr'   => $row["Gender"],
      'crse'   => $row["Course"],
      'yr'   => $row["Year"]
     );
    }
   $this->csv_import_model->insert($data);
  }


    }

它终于奏效了。