【发布时间】:2016-03-01 06:31:54
【问题描述】:
我需要在文件输入更改时在我的服务器中上传一个文件。我使用该库从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() 参数。谢谢 为了这个伟大的article!救了我的命!
【问题讨论】:
-
表单中缺少 enctype="multipart/form-data"
-
@VinodVT,我添加了
<form class="image" method="POST" enctype="multipart/form-data">,但我仍然得到错误 -
你没有遗漏表单的动作
-
我认为它不需要操作,因为它可以用我的脚本@AnmolRaghuvanshiVersion1.0 触发
标签: php codeigniter image-uploading