2013-04-13 23 views
0

我可以成功地將圖像上傳到我的笨控制器:我笨上傳圖片,但沒有Json的

class Upload extends CI_Controller { 

    function __construct() 
    { 
     parent::__construct(); 
     $this->load->helper(array('form', 'url')); 
     $this->load->database(); 
    } 

    function do_upload($folder) 
    { 
     $config['upload_path'] = './userdata/'. $folder . '/'; 
     $config['allowed_types'] = 'gif|jpg|png|txt'; 
     $config['max_size'] = '1000'; 
     $config['max_width'] = '5024'; 
     $config['max_height'] = '5768'; 

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

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

圖像是從我的JS上傳像這樣:

var formData = new FormData(); 
formData.append('userfile', this.image); 
$.ajax({ 
    url: 'upload/do_upload/atlas', 
    data: formData, 
    processData: false, 
    contentType: false, 
    type: 'POST', 
    success: function(data){ 

    } 
}); 

哪裏this.image是圖像文件。現在這工作正常,所以我知道我的控制器和代碼的作品。

但是,當我試圖用JSON字符串相同,該文件沒有上傳:

this.json = JSON.stringify(parsedJSON, null, 4); 

var formData = new FormData(); 
formData.append('userfile', this.json); 
$.ajax({ 
    url: 'upload/do_upload/json', 
    data: formData, 
    processData: false, 
    contentType: false, 
    type: 'POST', 
    success: function(data){ 

    } 
}); 

有誰知道爲什麼這個工程有圖像,但不是一個JSON字符串?

回答

0

您可以使用非標準過程JSON數據發送到服務器

var json = JSON.stringify(parsedJSON, null, 4); 

$.ajax({ 
    url: 'upload/do_upload/json', 
    data: json, 
    type: 'POST', 
    success: function(data){ 

    } 
}); 
+0

這是我做的基本上是什麼。當我這樣做時,我收到消息:XHR完成加載:「http://localhost/development/editor/index.php/upload/do_upload/json」。但沒有任何內容被上傳到json文件夾中 –

+0

你在firebug或chrome開發工具中看到了什麼 - 任何消息,錯誤,請求和響應。 – yAnTar