CI上传文件通过AJAX错误

时间:2016-09-08 06:04:15

标签: php jquery ajax codeigniter

我直接指向这里...我试图通过ajax和codeigniter上传来上传文件...但是,当我尝试上传具有正确文件扩展名的文件时,它给了我一个在文件的扩展名上的错误..我不知道是什么导致这个...我在我的本地运行该程序它工作正常..但是,当我在godaddy上传它时它给了我那个错误。 ..

显示错误:Unable to upload file other than csv(comma delimited) file extension!

但应该可以正常工作,因为我在此处上传了.csv文件。

这是我的代码:

JQUERY

$(document).on('submit', '#NewFileUploadForm', function(e){
   e.preventDefault();
   var $this = $(this);
   var $url_transaction = $this.attr('action');
   var formData = new FormData($this[0]);

   $(".st_upload_file").attr('disabled', 'disabled');
   $(".st_upload_file").html("<i class='fa fa-fw fa-spin fa-spinner'></i> Uploading...");

 $.ajax({
  xhr: function() {
  var xhr = new window.XMLHttpRequest();
  xhr.upload.addEventListener("progress", function(evt) {
    if (evt.lengthComputable) {
      var percentComplete = evt.loaded / evt.total;
      percentComplete = parseInt(percentComplete * 100);
      // console.log(percentComplete);
      $('#progress').width(percentComplete+"%");
      if (percentComplete === 100) {
        setTimeout(function(){
         $("#lblUpload").html("Complete!");
        }, 700);
      }

    }
  }, false);
  return xhr;
},
beforeSend:function(){
  n = new PNotify({
      title: 'Uploading File',
      text: '<label id="lblUpload">Processing</label>'+
            '<div class="progress">'+
              '<div id="progress" class="progress-bar progress-bar-success" data-transitiongoal="0" aria-valuenow="0" style="width: 0%;"></div>'+
            '</div>',
      hide: false,
      nonblock:{
        nonblock:true
      },
      styling: 'bootstrap3',
      addclass:'dark'
  });
},
url: $url_transaction+"Upload_sales_transaction_c/do_upload",
type: "POST",
data: formData,
dataType:'json',
contentType: false,
processData: false,
success: function(result) {
    if(result.is_status == 0){
         setTimeout(function(){
          location.reload();
           // $(".st_upload_file").removeAttr('disabled');
             $(".st_upload_file").html("Upload");
        }, 700);
    }else{

         $.ajax({
          url: $url_transaction+"Upload_sales_transaction_c/readExcel",
          type: 'post',
          data: {filename : result.filename, id : result.id},
          beforeSend:function()
          {
            getProgress("plog.txt", $this);
          },
          success: function(result){
              console.log(result);
            // if(result == 1){
                setTimeout(function(){
                     // $(".st_upload_file").removeAttr('disabled');
                     // $(".st_upload_file").html("Upload");
                     $.ajax({
                        url:$url_transaction+"Upload_sales_transaction_c/reset",
                        success:function(result){
                          location.reload();
                        }
                     });

                },700);
            // }
          }
        });
  }

 }

});
});

PHP代码

public function do_upload(){

    $year = $this->input->post('year');
    $month = $this->input->post('month');
    $filename = "upload_".md5(date('Y-m-d H:i:s'));
    $config['upload_path']      = './includes/uploaded_files/sales_transaction/';
    $config['allowed_types']    = 'csv';
    $config['max_size']         = 15000;
    $config['file_name']        = $filename; 

    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload('csvfile'))
    {
            $error = array('error' => $this->upload->display_errors());

            $sesssion_message = array(
            "type" => "warning",
            "message" => "Unable to upload file other than csv(comma delimited) file extension!"
            );
            $this->session->set_userdata($sesssion_message);
            // redirect(base_url()."upload_sales_transaction_c/");
            // exit;
            //0 - failed
            echo json_encode(array('is_status' => 0));
    }   
    else
    {
            $file = array('upload_data' => $this->upload->data());
            $prev_filename=$file['upload_data']['client_name'];


            $sesssion_message = array(
            "type" => "success",
            "message" => "File has been successfully uploaded!"
            );
            $this->session->set_userdata($sesssion_message);

            $insert = array('year' => $year, 'month' => $month, 'filename' => $filename, 'prev_filename' => $prev_filename);
            $insert_id = $this->main_m->insert_data('uploaded_sales_transaction_files', $insert);
            // redirect(base_url()."upload_sales_transaction_c/");
            // exit;
            //1 - success
            echo json_encode(array('is_status' => 1, 'id' => $insert_id, 'filename' => $filename));

    }

}

提前感谢...

0 个答案:

没有答案