2014-10-18 159 views
0

我是codeigniter的新手,並試圖上傳formfile字段。我成功地將沒有file的表單數據插入數據庫。但它顯示如下錯誤,當我嘗試用文件中的字段使用ajax將圖像上載到codeigniter中的圖像

`<p>You did not select a file to upload.</p>` 

我嘗試過很多辦法像https://www.daniweb.com/web-development/php/threads/477646/codeigniter-upload-form-with-ajaxCodeIgniter, Jquery Ajax, Form Submission and File Upload

但沒有什麼幫助了我。我的文件是(相關的文件上傳領域)。請幫我.. Thanku提前

view_add_author.php

<?php 
       $this->load->helper('form'); 
       echo form_open_multipart('admin/form_actions/add_author', 'id="add_author_form"'); 
       ?> 
<div class="form-group"> 
    <label for="author_image">Image</label> 
    <input type="file" name="author_image" id="author_image"> 
    <p class="help-block">Format(jpg, jpeg, gif, png)</p> 
</div> 

custom.js

function add_author(data){ 
return $.ajax({ 
    url: url+'admin/form_actions/add_author', 
    type: 'POST', 
    async: false, 
    dataType: 'json', 
    mimeType:"multipart/form-data", 
    data: data 
}); 
} 


$('#add_author_form').submit(function(e){ 
    e.preventDefault(); 
    var data = $('#add_author_form').serialize(); 
    add_author(data).done(function(data){ 
     if(data.msg != '') 
     { 
      $('#add_author_form')[0].reset(); 
      alert(data.msg); 
     } 
     else if(data.error != '') 
     { 
      alert(data.error); 
     } 
    }); 
}); 

form_actions.php

public function add_author() 
{ 
    $this -> load -> helper ('form'); 
    $config = array (
     'upload_path' => './images/author/', 
     'allowed_types' => 'gif|jpg|jpeg|png', 
     'max_size' => '2000', 
     'max_width' => '2000', 
     'max_height' => '2000', 
     'encrypt_name' => true, 
    ); 

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

    if (! $this -> upload -> do_upload ('author_image')) 
    { 
     echo $error = $this -> upload -> display_errors(); 
    } 
    else 
    { 
     echo $data = $this -> upload -> data(); 
    } 
} 

回答

0

我做了以下功能上傳圖像codeigniter使用upload library

通過使用此功能,我可以添加單個/多個文件

function add_image ($path, $field) //upload and the file field name 
{ 
    $config = array (
     'upload_path' => $path, 
     'allowed_types' => 'gif|jpg|jpeg|png', 
     'max_size' => '1024', 
     'max_width' => '1024', 
     'max_height' => '1024', 
     'encrypt_name' => TRUE 
    ); 
    $this->load->library ('upload', $config); //load codeigniter libraries 
    $name_array = array(); 
    $count = count ($_FILES[ $field ][ 'size' ]); 
    if (count ($_FILES[ $field ]) == count ($_FILES[ $field ], COUNT_RECURSIVE)) //if only one image is present 
    { 
     if (!$this->upload->do_upload ($field)) 
     { 
      return FALSE; 
     } 
     $data = $this->upload->data(); 
     return $data[ 'file_name' ]; //return the uploaded file name 
    } 
    else //if multiple images selected 
    { 
     foreach ($_FILES as $key => $value) 
     { 
      for ($s = 0; $s < $count; $s++) 
      { 
       $_FILES[ 'userfile' ][ 'name' ] = $value[ 'name' ][ $s ]; 
       $_FILES[ 'userfile' ][ 'type' ] = $value[ 'type' ][ $s ]; 
       $_FILES[ 'userfile' ][ 'tmp_name' ] = $value[ 'tmp_name' ][ $s ]; 
       $_FILES[ 'userfile' ][ 'error' ] = $value[ 'error' ][ $s ]; 
       $_FILES[ 'userfile' ][ 'size' ] = $value[ 'size' ][ $s ]; 
       if ($this->upload->do_upload()) 
       { 
        $data = $this->upload->data(); 
        $name_array[ ] = $data[ 'file_name' ]; 
       } 
       else 
       { 
        return FALSE; 
       } 
      } 
      return $name_array;//return the names of images uploaded 
     } 
    } 
} 
0

您可以使用下面的代碼

$(document).ready(function() { 
     // bind 'myForm' and provide a simple callback function 
     $('#add_author_form).ajaxForm(function() { 
      alert("Thank you for your comment!"); 
     }); 
    }); 

我希望你覺得它有用