我可以成功将图像上传到我的codeigniter控制器:
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 :(得分:0)
您可以使用标准流程将json数据发送到服务器
var json = JSON.stringify(parsedJSON, null, 4);
$.ajax({
url: 'upload/do_upload/json',
data: json,
type: 'POST',
success: function(data){
}
});