我可以成功地将图像上传到我的 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 字符串?