$ this-> upload-> do_upload()在使用ajax时不起作用

时间:2019-11-18 09:15:16

标签: php jquery ajax codeigniter

我有一个项目codeigniter,我将使用ajax进行表单提交。我的第一个问题是$this->upload->do_upload始终为false。我确实在寻找一些答案,但我无法完成工作。

我尝试了此链接来解决我的问题,但没有帮助。

希望有人可以帮助我。下面是我的代码。

查看

<form id="form-register" enctype="mutlipart/form-data" action="" method="POST">
      <div class="row">
         <div class="col-md-4">
             <div class="form-group">
                  <div class="form-group form-file-upload form-file-multiple ">
                     <input type="file" class="inputFileHidden"  name="client_img" id="client_img">
                     <div class="fileinput-new thumbnail img-raised">
                             img class="img-fluid" src="<? echo base_url();?>assets/img/person.png" alt="client-img" />
                     </div>
                     <div class="input-group mt-2">
                        <span class="input-group-btn">
                           <button type="button" class="btn btn-fab btn-round btn-primary">
                               <i class="material-icons">attach_file</i>
                            </button>
                         </span>
                         <input type="text" class="form-control inputFileVisible"  placeholder="Choose client picture.." require>
                     </div>
                  </div>
               </div>
             </div>

Controller.php

    public function register_client()
    {
        $validator = array('success' => false, 'messages' => array());

        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['encrypt_name'] = TRUE;

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

        if(!$this->upload->do_upload('client_img')){
            $validator['success'] = false;
            $validator['messages'] = $this->input->post('client_img');
        }else{
            $data = $this->upload->data();
            //Resize and Compress Image
            $config['image_library'] = 'gd2';
            $config['source_image'] = './uploads/'.$data['file_name'];
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = FALSE;
            $config['quality'] = '60%';
            $config['width'] = 600;
            $config['height'] = 400;
            $config['new_image'] = './uploads/'.$data['file_name'];

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

            $client_data = array(
                'account_no' => 1000,
                'client_img' => $data['file_name'],
                'mname' => $this->input->post('mname'),
                'gname' => $this->input->post('gname'),
                'lname' => $this->input->post('lname'),
                'email' => $this->input->post('email'),
                'number1' => $this->input->post('number1'),
                'number2' => $this->input->post('number2'),
                'purok_no' => $this->input->post('purok_no'),
                'barangay' => $this->input->post('barangay'),
                'city' => $this->input->post('city'),
                'postal_code' => $this->input->post('postal_code'),
                'birthdate' => $this->input->post('birthdate'),
                'gender' => $this->input->post('inlineRadioOptions'),
                'info' => $this->input->post('info')
            );

            $insert_data = $this->claims_model->insert_client($client_data);

            if($insert_data == true){
                $validator['success'] = true;
                    $validator['messages'] = 'loan';
            }else{

                $validator['success'] = false;
                $validator['messages'] = 'Something went wrong';    
            }
        }

        echo json_encode($validator);
    }

Ajax.js

$(document).ready(function(e) {
    $(".client-save").click(function(e) {
        e.preventDefault();
        var formdata = new FormData(document.getElementById("form-register"));

        if (formdata) {
            $.ajax({
                type: "POST",
                url: "register",
                dataType: "json",
                data: formdata,
                processData: false,
                contentType: false,
                cache: false,
                success: function(response) {
                    if (response.success == true) {
                        alert(response.messages);
                    } else {
                        showNotification("top", "right", response.messages);
                    }
                }
            });
        }
    });
});

希望有人可以解决我的问题。我尝试修改ajax代码,但仍然是

  

$ this-> upload-> do_upload()

仍然是错误的。

2 个答案:

答案 0 :(得分:1)

您可以调用display_errors()方法来帮助您进行调试,只需将其添加到返回响应的位置即可:

echo "<pre>";
   die(var_dump( $this->upload->display_errors() ));
echo "</pre>";

如果您使用的是Mac / Linux,请确保您具有对上载文件夹的写入权限,并且还应在写入之前检查它是否存在:

if (!is_dir('uploads')) {
    mkdir('./uploads', 0777, true);
}

答案 1 :(得分:0)

由于您需要附加图像文件,因此可以尝试以下代码

        var formData = new FormData();
        formData.append('client_img', $('#client_img')[0].files[0]);