codeigniter获取ajax帖子文件

时间:2015-12-06 06:34:42

标签: javascript php jquery ajax codeigniter

我正在尝试使用codeigniter中的ajax上传多个文件。我的ajax帖子工作正常,但我不知道如何在我的控制器中获取帖子文件 我尝试过这种方式,但它不起作用。请帮忙。谢谢。

HTML

                    <form method="post" name="upload_files" action="<?php echo base_url(); ?>home/upload_files" id="form_files" enctype="multipart/form-data">
                    <h4 style="float: left">Add files. (file types .png, .jpeg, .dwg, .pdf, jpg) max: 10Mb <span style="color:red;" id ="err_type"> * </span></h4>
                    <div class=from-section>
                                <div class="row">
                                    <div class="span4" id="image" >

                                        <div class="cart-total" id="clone_image" data-provides="fileupload">
                                                <div class="form-group has-success has-feedback">
                                                <input class="form-control" type="file" name="files[]" accept="image/*" />
                                                 </div>
                                        </div>

                                    </div><!-- /.span4 -->
                                </div><!-- /.row -->
                    </div>

                    <div class="cart-total">
                             <a id="another_image" class="btn btn-default" style="height: 45px;padding: 12px 12px; margin-right: 10px; ">+ Add More Files</a> 
                            <a id="submit_files" class="rx-bottom"   >Submit Files</a>

                    </div>


                </form>

AJAX

    <script type="text/javascript">


    $('#submit_files').click(function() {

    var formData = new FormData($('#form_files')[0]);

    $.ajax({
        url: "<?php echo base_url('home/upload_files/'); ?>",
        type: "POST",
        data: formData,
        mimeType: "multipart/form-data",
        processData: false,
        contentType: false,
        success: function(res) {
            console.log("success");
        },       
        error: function(res) {
            console.log("error");
         }       
         });

});

    </script>

我的控制台正在记录成功消息

CONTROLLER

    public function upload_files(){

     $order_id  = 123;

    if(!$order_id){
        redirect('home/instant_quote', 'refresh');
    }else{

        $this->load->library('upload');
            //$files = $this->input->post();
        $files = $_FILES;

       if($files){
            for( $i = 1; $i < 20; $i++) {

                    $config = array();
                    $config['upload_path'] = './uploads/';
                    $config['allowed_types'] = 'jpg|jpeg|png|pdf|dwg|zip';
                    $config['max_size']      = '11000';
                    $config['file_name'] = 'order_'.$order_id.'_'.$i;
                    $config['overwrite']     = false;

                    $this->upload->initialize($config);

                    $this->upload->do_upload('files'.$i);

                    $image_data = array('upload_data' => $this->upload->data());

                    if($image_data["upload_data"]["file_size"]){
                    $inserted['order']        = $i ;
                    $inserted['name']         = $image_data["upload_data"]["file_name"];
                    $inserted['extension']    = $image_data["upload_data"]["file_ext"];
                    $inserted['type']         = $image_data["upload_data"]["file_type"];
                    $inserted['size']         = $image_data["upload_data"]["file_size"];

                    $prop = $this->home->insert_files($inserted);
                    }
                }
        }else{
            $this->load->view('comman/header.php');
            $this->load->view('home/add_files.php');
            $this->load->view('comman/footer.php');
            }

        }
    }

这是我在记录表单数据时看到的内容

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以在控制器中使用它:

function upload(){
    if(!empty($_FILES)){
        $type= $_FILES["files0"]["type"]; //files0 is your input file name
        $size= $_FILES["files0"]["size"];

        $file_name = $_FILES["files0"]["name"];      
        $source = $_FILES['files0']['tmp_name'];
        $dir = "./uploads/";
        $file = $dir . $file_name;
        if(!file_exists ($file ))
        {
            move_uploaded_file($source,$file );
            exit("File has been uploaded");
        }
        else
        {
            echo "File exist";
        }
    }
    $this->load->view('v_test');
}

对于你的ajax jquery:

//add this to your head html
<script>
    var base_url = "<?php echo base_url(); ?>";
</script>

,这是你的ajax电话:

$("#submit_files").click(function() {
    var formData = new FormData($("#form_files")[0]);

    $.ajax({
        url: base_url + "home/upload_files",
        type: 'POST',
        data: formData,
        async: false,
        success: function (data) {
            alert(data)
        },
        cache: false,
        contentType: false,
        processData: false
    });

    return false;
});

请先测试单个文件,以及是否有效。尝试动态上传多个文件。希望这可以帮到你。 :d