【问题标题】:Codeigniter jQuery file upload not workingCodeigniter jQuery文件上传不起作用
【发布时间】:2017-03-29 06:16:19
【问题描述】:

PHP 代码:

$files = json_decode($this->input->post('Image'));
echo "<pre>";
print_r($files);
echo "</pre>";

jQuery 代码:

var image = [];
for(var i = 1; i <= $("#removeimagefield").attr('data-id'); i++){
    image.push($("#imageupload"+i).val());
}
console.log(JSON.stringify(image));

HTML 代码:

<form role="form" enctype="multipart/form-data" accept-charset="utf-8" name="formname" id="formname"  method="post" action="">
    <input type="file" class="default" id="imageupload1">
</form>

AJAX 功能:

  addproperty = {Image: JSON.stringify(image)}
  $.ajax({
    method: "POST",
    url: formsubmission,
    data: addproperty,
    async : false,
    cache : false,
    contentType : false,
    processData : false,
    success: function(response){
      console.log(response);
    }
  });

控制台消息:

["C:\\fakepath\\abhishek.jpg","C:\\fakepath\\krunal.jpg","C:\\fakepath\\nitin.jpg"]

我正在尝试将图像文件上传到 Codeigniter 控制器。但是当我打印 $files 的值时。比它显示空数组。

【问题讨论】:

  • 控制器中的代码在哪里? ://

标签: php jquery codeigniter


【解决方案1】:

试试这个代码,用简单的 $_FILES['img_file']['file_name'] 把你的文件放到控制器中

   $(document).ready(function (e) {
                    var file_name = $('#id_file_input').prop('files')[0];
                    var form_data = new FormData();
                    form_data.append('img_file', file_data);
                    $('#vid_file').prop('disabled', true);
                    $.ajax({
                    url: '<?php echo base_url(); ?>Controller/function_name', 
                        dataType: 'text', 
                        cache: false,
                        contentType: false,
                        processData: false,
                        data: form_data,
                        type: 'post',
                          success: function (response) {

                        }
                    });
                    });

【讨论】:

  • 人的一切都是动态的,不是静态的。人们可以为图像添加新字段。
猜你喜欢
  • 1970-01-01
  • 2013-03-17
  • 2013-12-09
  • 2018-02-19
  • 2016-02-20
  • 2012-03-22
  • 2017-12-17
  • 1970-01-01
  • 2015-09-26
相关资源
最近更新 更多