weixin_33734785 2016-09-08 06:04 采纳率: 0%
浏览 17

CI通过AJAX上传文件错误

I'm going straight to the point here... I'm trying to upload file here via ajax and codeigniter upload... however, when I tried to upload the file that has the correct file extension it gives me an error which on the extension of the file.. I don't know what causes this.. I run the program on my local it worked fine.. however, when I uploaded it on godaddy it gives me that error...

the error displayed: Unable to upload file other than csv(comma delimited) file extension!

but should work fine because I uploaded a .csv file here.

Here's my code:

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 CODE

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));

    }

}

thanks in advance...

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!