upload image using codeigniter with ajax and formdata

前端 未结 3 1394
栀梦
栀梦 2020-12-20 07:40

I like to upload image using codeigniter Framework in PHP with JQuery AJAX without redirecting the page. When i upload the image it redirecting to controller area and the v

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-20 08:19

    Try This. It's working-
    HTML Form-

    Ajax -

    $('#submit').submit(function(e){
        e.preventDefault(); 
             $.ajax({
                 url:'Your path',
                 type:"post",
                 data:new FormData(this),
                 processData:false,
                 contentType:false,
                 cache:false,
                 async:false,
                  success: function(data){
                      alert(data);
               }
             });
        });  
    

    Just set url in ajax, It would work perfectly.
    Controller function-

    public function do_upload(){
            $config['upload_path']="./upload";
            $config['allowed_types']='gif|jpg|png';
            $this->load->library('upload',$config);
            if($this->upload->do_upload("file")){
            $data = array('upload_data' => $this->upload->data());
            $data1 = array(
            'menu_id' => $this->input->post('selectmenuid'),
            'submenu_id' => $this->input->post('selectsubmenu'),
            'imagetitle' => $this->input->post('imagetitle'),
            'imgpath' => $data['upload_data']['file_name']
            );  
            $result= $this->Admin_model->save_imagepath($data1);
            if ($result == TRUE) {
                echo "true";
            }
            }
    
         }
    

提交回复
热议问题