title: How can i upload image with ajax in codeigniter?
body: <p>I'm trying to upload image with jQuery and ajax function, and how can I fetch all details of image file, like in php we use <code>$_FILE()</code></p>
  
  <p>Here is my code</p>
  
  <p><strong>JS</strong></p>
  
  <pre><code>$("#uploadimg" ).click(function() {
       $( "#file" ).click();
  });
  
  $( "#file" ).change(function(e) {
      var file=$('#file').val();
      alert(file);
      die();
      e.preventDefault();
      $.ajax({
          url:'http://localhost/JSG/blog/uploadimg',
          secureuri:false,
          type: "POST",
          fileElementId:'image',
          dataType: 'text',
          data:{ file: file },
          cache: true,
          success: function (data){
              alert(data);
              console.log(data);
          },
      });
  });
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public function uploadimg()
  {
      $var = $_POST['file'];
      print_r($var);
      if($this-&gt;input-&gt;post('file')) {
          $config['upload_path'] = 'upload'; 
          $config['file_name'] = $var;
          $config['overwrite'] = 'TRUE';
          $config["allowed_types"] = 'jpg|jpeg|png|gif';
          $config["max_size"] = '1024';
          $config["max_width"] = '400';
          $config["max_height"] = '400';
          $this-&gt;load-&gt;library('upload', $config);
  
          if(!$this-&gt;upload-&gt;do_upload()) {
              $this-&gt;data['error'] = $this-&gt;upload-&gt;display_errors();
              print_r( $this-&gt;data['error']);
          } else {
              print_r("success");
          }
      }
  }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>&lt;form role="form"&gt;
      &lt;div class="form-group"&gt;
          &lt;label for="recipient-name" class="control-label"&gt;Blog Title:&lt;/label&gt;
          &lt;input type="text" class="form-control" id="recipient-name"&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;label for="message-text" class="control-label"&gt;Message:&lt;/label&gt;
          &lt;textarea class="form-control" id="message-text"&gt;&lt;/textarea&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;label for="message-text" class="control-label"&gt;Upload image:&lt;/label&gt;
          &lt;img src="&lt;?php echo base_url();?&gt;assest/img/blank.png" alt="Blank image" id="uploadimg" class="img-thumbnail"&gt;
          &lt;input style="display:none" id="file" value=" " type="file" class="file" data-show-preview="false"&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>Response</strong></p>
  
  <blockquote>
    <p>C:\fakepath\Koala.jpg You did not select a file to upload.</p>
  </blockquote>
  
  <p>Please help</p>