[英]Codeigniter file upload doesn't get to controller
关于文件输入更改,我需要在服务器中上传文件。 我使用该库从Codeigniter输入文件。
指数
<form class="image" method="POST">
<input id="image-value" data-id="{{id}}" name="{{picture}}" class="image-box-{{id}}" data-type="{{type}}" type="file" value="{{picture}}" />
</form>
这是我制作接受图像的表格的视图
脚本
$('[id="image-value"]').change(function(e) {
var file_data = $(this).prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({ // 02102016 AA: picture_caption
url: './upload_picture/',
dataType: 'json',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(data){
alert(data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.error("The following error occured: " + textStatus, errorThrown);
}
});
触发后,脚本将从索引中获取文件并将其发送到我的控制器
控制者
function __construct() {
parent::__construct();
$config['upload_path'] = './data/picture/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
$this->upload->initialize($config);
}
function upload_picture() {
// $data = Array();
log_message("debug", "do_upload: ".$this->upload->do_upload());
if ($this->upload->do_upload()) {
log_message("debug", "data: ".$this->upload->data());
}
问题(我认为)是我如何从控制器传递数据,因为即使从脚本传递了数据, do_upload
的值仍然为false
原来只是需要
"file"
作为do_upload()参数。 感谢这篇伟大的文章 ! 救了我!
包含函数名称,但不包含控制器名称..
$('[id="image-value"]').change(function(e) {
var file_data = $(this).prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({ // 02102016 AA: picture_caption
url: 'controllerName/upload_picture/', //add the controller.
dataType: 'json',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(data){
alert(data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.error("The following error occured: " + textStatus, errorThrown);
}
});
控制者
$var = $_POST['file'];
print_r($var);
if($this->input->post('file')) {
$config['upload_path'] = 'upload';
$config['file_name'] = $var; // this may be needed
$config['overwrite'] = 'TRUE';
$config["allowed_types"] = 'jpg|jpeg|png|gif';
$config["max_size"] = '1024';
$config["max_width"] = '400';
$config["max_height"] = '400';
$this->load->library('upload', $config);
if(!$this->upload->do_upload()) {
$this->data['error'] = $this->upload->display_errors();
print_r( $this->data['error']);
} else {
print_r("success");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.