2012-07-21 150 views
1

我不知道我在做什麼錯在這裏。文件上傳Codeigniter和ajax

控制器

function do_test() 
{ 
    $config['upload_path'] = './images'; 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = '10000'; 
    $config['max_width'] = '1024'; 
    $config['max_height'] = '768'; 
    $config['overwrite'] = TRUE; 
    $confit['remove_spaces'] = TRUE; 

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

    $this->form_validation->set_rules('file', 'trim|required|xss_clean'); 
    $test = $this->input->post('file'); 
    var_dump($test); //var dump return : string(21) "C:\fakepath\truth.jpg" 

    if (! $this->upload->do_upload($test)) 
    { 
     echo "no"; 
    } 
    else 
    { 
     $data = array('upload_data' => $this->upload->data()); 

     echo "yes"; 
    } 
} 

形式

<?php echo form_open_multipart('home/do_test', array('id' => 'skill-form-test'));?> 

    <input type="file" name="userfile" id="file" size="20" /> 

    <br /><br /> 

    <input type="submit" value="upload" /> 

    </form> 

的jquery

$('#skill-form-test').submit(function(e) { 
    e.preventDefault(); 

    $.post(base_url + "index.php/home/do_test", { file : $("#file").val() }, function(data) 
     { 
      }, "json"); 
    alert ($("#file").val() + "-" + base_url); //This alerts: C:\fakepath\truth.jpg-http://siteurl.com/ 
}); 
+1

我不確定你在這裏問什麼。簡而言之,這有什麼問題? – itachi 2012-07-21 03:52:44

+0

爲什麼圖像文件無法上傳? – Ciprian 2012-07-21 03:55:48

+0

@ciprian爲什麼使用'json'數據類型上傳圖片? – undefined 2012-07-21 04:14:20

回答

0

只要改變

if (! $this->upload->do_upload($test)) 

if (! $this->upload->do_upload('userfile')) 

將解決此問題。

希望這可以幫助你。謝謝!!

+1

嘗試...不起作用。 – Ciprian 2012-07-21 05:57:29

+0

@ciprian,除非你使用'flash/swfplugin'或'iframe',否則Ajax文件上傳是不可能的。我寫定製ajax文件上傳,並在這裏回答http://stackoverflow.com/questions/11556199/ajax-file-upload-using-codeigniter/11557399#11557399你可以看看它。 – 2012-07-21 06:19:31