文件上传不适用于ajax和codeigniter

时间:2015-12-11 05:09:02

标签: ajax codeigniter codeigniter-2 codeigniter-3

我有一个添加form.added数据使用ajax和codeigniter.there是一个文件上传的形式。但文件没有上传。其他数据被添加。但文件没有上传到指定的文件夹。

查看档案

     <div class="row">

    <div class="col-xs-6">
        <label for="txtname">Title of Quotation Request :</label>
        <input type="text" name="txtTitle" class="form-control" id="txtname"  value="<?php
        if (!empty($service)) {
            echo $service;
        }
        ?>"  required>
    </div>

    <div class="col-xs-6">
        <label for="txtcustomer">Select Customer :</label>

        <select class="form-control" name="customer" id="customer" required="required">
            <option value="">----Select------</option>
            <?php
            foreach ($customers as $customer) {
                echo ' <option value="' . $customer->usr_id . '">' . $customer->usr_name . '</option>';
            }
            ?>
        </select>
        <input type="hidden" name="" value="sbMerchant">
    </div>
    <div class="col-xs-12">
        <label for="txtattachments">Drawing Attachments :</label>
        <input name="txtattachments" type="file" id="txtattachments">
    </div>

Ajax功能

<script type="text/javascript">
    $('#rfqsubmit').click(function () {
        //var title = $('#title').val();
     alert($('#txtattachments').val());
        var form_data = {
            title: $('#txtname').val(),
            merid: $('#mermerchant').val(),
            userid: $('#customer').val(),
            description: $('#txtrequirement').val(),
            reqid: $('#requirementid').val(),
            shipmethod: $('#shipmethod').val(),
            shiplocation: $('#shiplocation').val(),
            txtattachments: $('#txtattachments').val(),
            bidclose: $('#txtbidclose').val(),
            shipcurrency:$('#shipcurrency').val(),
            txtproduct:$('#txtproduct').val(),
            txtunit:$('#txtunit').val(),
            txtquantity:$('#txtquantity').val(),
            ajax: '1'
        };

        $.ajax({
            url: "<?php echo base_url() ?>moderator/RFQ/addoffline",
            type: 'POST',
            data: form_data,
            dataType:"Json",

            success: function(data) {
                var last_inserted_id = data.id;
                window.location.href ="<?php  echo base_url() ?>moderator/RFQ/viewrfq/"+last_inserted_id;
                // window.location.href ="<?php //echo base_url()             ?>moderator/RFQ/viewrfq/"+  form_data.reqid;
//      alert('added Successfully');
            }
        });

        return false;
    });



</script>

控制器

public function addoffline() {
        $this->load->helper(array('form', 'url'));
        $this->load->helper('file');
         $ip = $_SERVER['REMOTE_ADDR'];
         $config['upload_path'] = 'assets/images/rfqimages';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000';
        $config['max_width'] = '2024';
        $config['max_height'] = '1768';
        $config['width'] = 75;
        $config['height'] = 50;
         if (isset($_FILES['txtattachments']['name'])) {
            $filename = "-" . $_FILES['txtattachments']['name'];
            $config['file_name'] = substr(md5(time()), 0, 28) . $filename;
        }
        $config['overwrite'] = TRUE;
        $config['remove_spaces'] = TRUE;
        $field_name = "txtattachments";
        $this->load->library('upload', $config);
         if (!$this->upload->do_upload('txtattachments')) {
                //no file uploaded or failed upload
                $error = array('error' => $this->upload->display_errors());
            } else {
                $dat = array('upload_data' => $this->upload->data());
                $this->resize($dat['upload_data']['full_path'], $dat['upload_data']['file_name']);
            }
        $data7 = array(
            'rfq_title' => $this->input->post('title'),
            'rfq_detail' => $this->input->post('description'),
            'rfq_merchantid' => $this->input->post('merid'),
            'rfq_userid' => $this->input->post('userid'),  
            'rfq_requirementid'=>$this->input->post('reqid'),  
            'rfq_shipmethod'=>$this->input->post('shipmethod'), 
            'rfq_shiplocation'=>$this->input->post('shiplocation'), 
            'rfq_bidclosing'=>strtotime($this->input->post('bidclose')), 
            'rfq_shipcurrency'=>$this->input->post('shipcurrency'),
            'rfq_productid'=>$this->input->post('txtproduct'),
            'rfq_unit'=>$this->input->post('txtunit'),
            'rfq_quantity'=>$this->input->post('txtquantity'),
            'rfq_resource'=>2,
            'rfq_dated'=>time(),
            'rfq_status'=>0,
            'rfq_ipadd'=>$ip
        );

         $inserted_id= $this->requirement_model->forminsert($data7);
         $response=array('id'=>$inserted_id,'message'=>"inserted successfully"); 
         echo json_encode($response); 
         die();

    }

2 个答案:

答案 0 :(得分:0)

不要选择那样的字段,而是喜欢这个

var form_data = new FormData("id of the foem");

然后在这样的ajax中传递它,

data: form_data,

确保为表单设置了enctype

enctype="multipart/form-data"

在编码器中,

<?php echo form_open_multipart('moderator/RFQ/addoffline');?>

答案 1 :(得分:0)

试试这个:

var formData = new FormData($(this)[0]);

$.ajax({
    url: "<?php echo base_url() ?>moderator/RFQ/addoffline",
    type: "POST",
    data: formData,
    async: false,
    success:function(data){
        var last_inserted_id = data.id;
        window.location.href ="<?php  echo base_url() ?>moderator/RFQ/viewrfq/"+last_inserted_id;
        }
    },
    cache: false,
    contentType: false,
    processData: false
});

使用查看addoffline()操作中的数据 print_r($this->input->post())print_r($_FILES)函数。根据您的输出,您可以实现您的代码。 你的问题将得到解决。