Codeigniter jQuery文件上传不起作用

时间:2017-03-29 06:16:20

标签: php jquery codeigniter

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 code:

<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 Controller。但是当我打印$ files的价值时。而不是显示空数组。

1 个答案:

答案 0 :(得分:0)

尝试使用此代码并使用简单的$ _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) {

                        }
                    });
                    });