Codeigniter文件表单上传

时间:2016-10-11 22:01:55

标签: javascript php file codeigniter upload

当我尝试更新表单上的文件时出现此错误...“您没有选择要上传的文件。” ...问题是我真的在选择要上传的新文件...请帮助我,我不明白发生了什么......这是我的代码:

FORM

<form class="col s12" id="update_form" required="" enctype="multipart/form-data" aria-required="true" name="update_form" method="post" >
      <div class="row">
        <div class="input-field col s6">
          <input id="update_name" type="text" required="" aria-required="true" name="name" class="validate">
          <label for="first_name">Nombre</label>
        </div>
        <div class="input-field col s6">
          <input id="update_last_name" name="lastname" required="" aria-required="true" type="text" class="validate">
          <label for="last_name">Apellido</label>
        </div>
      </div>
      <div class="row">
        <div class="input-field col s6">
          <input id="update_side" type="text" required="" aria-required="true" name="side" class="validate">
          <label for="partido">Partido</label>
        </div>
        <div class="input-field col s6">
          <input id="update_charge" type="text" required="" aria-required="true" name="charge" class="validate">
          <label for="cargo">Cargo</label>
        </div>
      </div>
        <div class="row">
            <div class="input-field col s6">
               <div class="file-field input-field no-margin-top">
                  <div class="btn light-blue darken-4">
                    <span>Animación/Imagen</span>
                    <input type="file" name="animation">
                  </div>
                  <div class="file-path-wrapper">
                    <input class="file-path validate" id="animation" name="animation" type="text">
                  </div>
                </div>
            </div>

            <div class="input-field col s6">
            <select id="update_section" required="" aria-required="true" name="section" autocomplete="off">
              <option value="" disabled selected>Seleccione una opción</option>
              <option value="1">Presidencia</option>
              <option value="2">Senadores</option>
              <option value="3">Diputados</option>
            </select>
            <label>Sección</label>
          </div>
        </div>
        <input type="hidden" name="update_politic_hide" id="update_politic_hdn" value="">
    </form>

CONTROLLER

public function update_politic(){

  $this->load->model("politic");
  $params;

  if ($this->input->is_ajax_request()) {

    if (empty($this->input->post("animation"))){
        echo "first";
          $data = $this->politic->get_file_name($this->input->post("update_politic_hide"));
          $file = $data->POLITIC_FILE;//recupero el nombre de la imagen

          $params["name"] = $this->input->post("name");
          $params["lastname"] = $this->input->post("lastname");
          $params["side"] = $this->input->post("side");
          $params["charge"] = $this->input->post("charge");
          $params["section"] = $this->input->post("section");
          $params["animation"] = $file;
          $params["id"] = $this->input->post("update_politic_hide");

          if ($params["section"]=="Presidencia") {
              $params["section"]=1;
          }

          if ($params["section"]=="Senadores") {
              $params["section"]=2;
          }

          if ($params["section"]=="Diputados") {
              $params["section"]=3;
          }
    }
    else {
                  echo "second";
                $config['upload_path'] = "./public/uploads/";
                $config['allowed_types'] = "*";
                //$config['overwrite'] = "true";
                $config['max_size'] = "500000";
                $config['max_width'] = "2000";
                $config['max_height'] = "2000";

                $this->load->library('upload', $config);
                //$file = "animation";

                if (!$this->upload->do_upload("animation")) {
                  $data['uploadError'] = $this->upload->display_errors();
                  echo $this->upload->display_errors();
                }
          else {

                      $file_info = $this->upload->data();
                      $params["name"] = $this->input->post("name");
                      $params["lastname"] = $this->input->post("lastname");
                      $params["side"] = $this->input->post("side");
                      $params["charge"] = $this->input->post("charge");
                      $params["animation"] = $file_info['file_name'];
                      $params["section"] = $this->input->post("section");
                      $params["id"] = $this->input->post("update_politic_hide");

                      if ($params["section"]=="Presidencia") {
                          $params["section"]=1;
                      }

                      if ($params["section"]=="Senadores") {
                          $params["section"]=2;
                      }

                      if ($params["section"]=="Diputados") {
                          $params["section"]=3;
                      }

          }
        }

        $this->politic->update($params);
  }
}

MODEL

public function update($param){

  $id = $param["id"];
    $values = array(

                    "POLITIC_NAME" => $param["name"],
                    "POLITIC_LASTNAME" => $param["lastname"],
                    "POLITIC_SIDE" => $param["side"],
                    "POLITIC_CHARGE" => $param["charge"],
                    "POLITIC_FILE" => $param["animation"],
                    "SECTION_ID" => $param["section"],
                    );

  $this->db->where("POLITIC_ID",$id);
    $this->db->update("politics",$values);

}

JAVASCRIPT

$("#update_politic_btn").click(function(event) {
    /* Act on the event */

    var chango = $("#update_form").serialize();
    $.post(baseurl + 'admin/update_politic', chango,
        function(data) {
            console.log(data);
            list_politic();
        });
    event.preventDefault();
});


function update_politic(id, name, lastname, section, side, charge, file) {

    $("#update_politic_hdn").val(id);
    $("#update_name").val(name);
    $("#update_last_name").val(lastname);
    $("#update_side").val(side);
    $("#update_charge").val(charge);
    $('[name=section]').val(section);
    $("#animation").val(file);

    $('select').material_select();

}

2 个答案:

答案 0 :(得分:1)

有两个名称属性具有相同的值animation

<input type="file" name="animation">

<input class="file-path validate" id="animation" name="animation" type="text">

第二个name属性覆盖了第一个属性。你需要给它一个不同的名字。

答案 1 :(得分:0)

更改您的Ajax代码:

$("#update_politic_btn").click(function(event) {
var chango = new FormData($("#update_form")[0]);
// try this if above one not work
//var chango = new FormData($("#update_form"));
$.ajax({
    url: baseurl + 'admin/update_politic',
    type: 'POST',
    data: chango,
    async: false,
    success: function (data) {
        console.log(data);
        list_politic();
    },
    cache: false,
    contentType: false,
    processData: false,
    error : function() {

    }
});
event.preventDefault();
});