Codeigniter:通过Ajax上传图像并存储在数据库中

Codeigniter: Uploading image through Ajax and storing in db

本文关键字:存储 数据库 图像 通过 Ajax Codeigniter      更新时间:2023-09-26

我使用的是CI 3.0.1,在我使用ajax之前,我成功地将图像上传并插入数据库,我想在尝试使用ajax时,我错过了一些甚至不能发送数据上传的东西,可能是因为我们必须在表单中使用multipart(),而在ajax中,我们只是将数据直接发送到控制器,还有一件事我不知道如何接收响应中的变量
我的Ajax请求函数是:

<script type="text/javascript">
 $(document).ready(function() {
 alert("thirddddddddd");
        $('#btnsubmit').click(function() 
        {
            alert("i got submitted");
            event.preventDefault();
             var userfile = $("input#pfile").val();
                        alert("uploading");
    $.ajax({
        url: '<?php echo base_url(); ?>upload/do_upload', //how to receive var here ?
            type: 'POST',
             cache: false,
             data: {userfile: userfile},
            success: function(data) {
            alert(data);
            $('.img_pre').attr('src', "<?php echo base_url().'uploads/' ?>");
                $('.dltbtn').hide();
            },
            error: function(data)
            {
            console.log("error");
            console.log(data);
               alert("Error :"+data);
            }
        });
    });
});

我的控制器Upload的函数do_Upload是:

 public function do_upload()
    {
            $config['upload_path']          = './uploads/'; #$this->config->item('base_url').
            $config['allowed_types']        = 'gif|jpg|png|jpeg';
            $this->upload->initialize($config);
            if ( ! $this->upload->do_upload('userfile'))
            {
                    $error = array('error' => $this->upload->display_errors());
                    $this->load->view('layouts/header');
                    $this->load->view('home_page', $error);
                    $this->load->view('layouts/footer');
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());
                    $imagedata = $this->input->post('uerfile');
                    $session_data = $this->session->userdata('logged_in');
                    $id =  $session_data['id'];

                    $result = $this->model_edit->update_dp($id, $imagedata);
                    $image_name = $result->images;
                    $this->session->set_userdata('image', $image_name);
                    echo json_encode($name = array('image' => $image_name));
                    // $image_name = $this->upload->data('file_name');
                    // $data = array('upload_data' => $this->upload->data());
                    // redirect("account");
            }
    }

现在图像甚至不会被上传到文件夹,如果需要任何其他文件,请告诉我我会在这里发布。感谢

不能使用$("input#pfile").val(); 发送文件数据

var len = $("#pfile").files.length;
    var file = new Array();
    var formdata = new FormData();
    for(i=0;i<len;i++)
    {
        file[i] = $("input#pfile").files[i];
        formdata.append("file"+i, file[i]);
    }

并从ajax 发送formdata作为数据

希望它能有所帮助!

尝试使用以下ajax代码

var formData = new FormData($('#formid'));
$.ajax({
    url: '<?php echo base_url(); ?>upload/do_upload',
    data: formData ,
    cache: false,
    contentType: false,
    processData: false,
    type: 'POST',
    success: function(data){
        alert(data);
    }
});

根据您的代码,文件内容可能不会通过ajax发送。尝试使用上面代码中提到的属性。