【问题标题】:Retrieve file list passed by ajax call to a php controller [duplicate]检索通过ajax调用传递给php控制器的文件列表[重复]
【发布时间】:2019-08-10 17:00:08
【问题描述】:

我已经使用 ajax 调用将两个字符串和一个列表传递给控制器​​。从控制器我可以检索我的字符串值。但是我传递的列表是一个对象。如何将该对象转换为列表。

这是我的 ajax

var imagelist = [];
if ('files' in x) {
    if (x.files.length == 0) {
        alert("Add one or more images");
        ss
    } else {
        for (var i = 0; i < x.files.length; i++) {

            imagelist.push(x.files[i]);

        }
    }
    jQuery.ajaxSettings.traditional = true;
    $.ajax({
        url: url_path,
        type: 'POST',
        data: {
            'pname': pname,
            'des': des,
            'ilist': imagelist
        },
        success: function(data) {
            alert("test");
        },
        error: function(request, error) {
            alert("error");
        }
    });
}

控制器是

 $images = $this->input->post('ilist');

    foreach($images as $img){


    }

这是 $images 得到的值

 [object File]

【问题讨论】:

  • 试试 $images = json_decode($this->input->post('ilist'), TRUE);将列表作为数组获取。
  • 它什么也不返回。
  • 如果你在javascript中做console.log。它显示了什么?
  • console.log(imagelist) 显示 [File, File] 因为我选择了两个图像
  • 您是要上传文件,还是只发送文件名?

标签: javascript php ajax codeigniter


【解决方案1】:

尝试使用 formdata 对象发送图像

【讨论】:

  • 嗨。如果一个问题有重复,那么该问题应该作为重复关闭而不是再次回答。此外,答案应该包含适当的代码示例,而不仅仅是“尝试使用这个或那个”。展示如何他们可以使用“this”或“that”来解决问题。
猜你喜欢
  • 2020-01-27
  • 1970-01-01
  • 2020-02-26
  • 2014-07-07
  • 2015-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-08
相关资源
最近更新 更多