【问题标题】:Codeigniter file upload doesn't get to controllerCodeigniter 文件上传无法到达控制器
【发布时间】: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,我添加了&lt;form class="image" method="POST" enctype="multipart/form-data"&gt;,但我仍然得到错误
  • 你没有遗漏表单的动作
  • 我认为它不需要操作,因为它可以用我的脚本@AnmolRaghuvanshiVersion1.0 触发

标签: php codeigniter image-uploading


【解决方案1】:

包含函数名称但不包含控制器名称..

$('[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");                                      
    }

【讨论】:

  • 事实并非如此。我的控制器在显示日志时被调用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-27
  • 1970-01-01
  • 2021-10-29
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 2020-02-05
相关资源
最近更新 更多