Upload an image by Ajax and CI

0

I want to know if my JQuery is well declared or set.

In the capture part I capture it for the .val() nose if it will be bad or good, because at the time of recording it says that no image has been selected

MTML:

    <div class="row">
                     <label align="center" class="control-label col-md-12">Imagen</label>
                      <div class="col-md-12">
                         <div class="form-group">
                           <div id="form_derivacion" class="form-group col-sm-12">
                              <input id="foto_empleado" name="foto_empleado" type="file" data-show-upload="false" class="file"  required="true" accept="image/*">
                           </div>
                        </div>
                     </div>
  </div>

JQUERY

function guardar_compra() {

    var numero_compra = $('#numero_compra').val();
    var id_proveedor = $('#id_proveedor').val();
    var monto_total = $('#monto_total').val();
    var fecha_compra = $('#fecha_compra').val();
    var foto_empleado = $('#foto_empleado').val();

        var dataString = 'numero_compra=' + numero_compra
                + '&id_proveedor=' + id_proveedor
                + '&monto_total=' + monto_total
                + '&fecha_compra=' + fecha_compra
                + '&foto_empleado=' + foto_empleado;

        $.ajax({
            type: 'POST',
            url: baseurl + 'Compras/guardar_compra',
            data: dataString,
            cache: false,
            contentType: false,
            processData: false,
            success: function (result) {
                var resultado = $.trim(result);
                if (resultado == 1) {
                    $("#modal_form_producto").modal('hide');
                    $('#notificacion').modal('show');
                    $('#msg_respuesta').html("<strong><FONT color='#1b5e20' SIZE=6>Producto Guardado</FONT>");
                    limpiar();
                    reload_tabla_pagos();
                } else {
                    alert("Error!" + result);
                }
            },
            error: function (result) {
            }
        });
    }
}

This is my driver:

public function guardar_compra() {
        $numero_compra = $this->input->post('numero_compra');
        $id_proveedor = $this->input->post('id_proveedor');
        $monto_total = $this->input->post('monto_total');
        $fecha_compra = $this->input->post('fecha_compra');
        $config = [
            "upload_path" => "./assets/images",
            'allowed_types' => "png|jpg"
        ];

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

        if ($this->upload->do_upload('foto_empleado')) {
            $data = array("upload_data" => $this->upload->data());
            $datos = $data['upload_data']['file_name'];
            if ($this->compras->guardar_compra($numero_compra, $id_proveedor, $monto_total, $fecha_compra, $datos) == true)
                echo "exito";
            else
                echo "error";
        }
        else {
            echo $this->upload->display_errors();
        }

MY MODEL:

public function guardar_compra() {
        $numero_compra = $this->input->post('numero_compra');
        $id_proveedor = $this->input->post('id_proveedor');
        $monto_total = $this->input->post('monto_total');
        $fecha_compra = $this->input->post('fecha_compra');
        $config = [
            "upload_path" => "./dist/img/compras",
            'allowed_types' => "png|jpg"
        ];

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

        if ($this->upload->do_upload('file')) {
            $data = array("upload_data" => $this->upload->data());
            $datos = $data['upload_data']['file_name'];
            if ($this->compras->guardar_compra($numero_compra, $id_proveedor, $monto_total, $fecha_compra, $datos) == true)
                echo "exito";
            else
                echo "error";
        }
        else {
            echo $this->upload->display_errors();
        }
    }
    
asked by Joel More F. 21.06.2017 в 00:06
source

1 answer

1

to upload files is not as straightforward as sending a variable.

This is an example of how to send the file using FormData () that I took from this question Check the answer of @pedrozopayares that has less complexity to implement. In the same response it shows the php code to receive the file.

EDITION 1

To upload the rest of the fields are added to the formData.

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

formData.append('numero_compra', numero_compra);
formData.append('id_proveedor', id_proveedor);
formData.append('monto_total', monto_total);
formData.append('fecha_compra', fecha_compra);

$.ajax({
       url : 'upload.php',
       type : 'POST',
       data : formData,
       processData: false,  // tell jQuery not to process the data
       contentType: false,  // tell jQuery not to set contentType
       success : function(data) {
           console.log(data);
           alert(data);
       }
});
    
answered by 21.06.2017 / 06:14
source