微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 通过Ajax上传图像时出错

我无法通过ajax上传多个文件.这是我的代码.

HTML代码: –

   <input type="file" id="txtBusinessImage" class="form-control" name="txtBusinessImageName[]" multiple >

    <input type="hidden" id="selectBusinessHiddenID" name="selectBusinessHiddenID" value="<?PHP echo $viewCompanyResult->company_id; ?>">

    <input type="button" id="uploadBusinessImg" value="Upload" >

Ajax代码: –

$("#uploadBusinessImg").on("click",function(e)
{
                var fd = new FormData();
                var file_data = $("#txtBusinessImage")[0].files; // for multiple files
                for(var i = 0;i<file_data.length;i++){
                    fd.append("file"+[i], file_data[i]);
                }
                var other_data = $("#selectBusinessHiddenID").serializeArray();
                $.each(other_data,function(key,input){
                    fd.append(input.name,input.value);
                });

                $.ajax({
                    url: '<?PHP echo site_url('Main_ctrl/upload_business_photo_do'); ?>',
                    data: fd,
                    enctype: 'multipart/form-data',
                    contentType: false,
                    processData: false,
                    type: 'POST', async : true,
                    success: function(data){
                        alert(data);
                    }
                });
});

当我通过Ajax调用upload_business_photo_do()函数时,它无法恢复图像的名称$_FILES [‘file’] [‘name’]

upload_business_photo_do()
{
     $business_hidden_id=$this->input->post('selectBusinessHiddenID');

        /*code for image*/
        $config['upload_path']='./upload_101/';
        $config['allowed_types']= 'jpg|png|jpeg';
        $config['max_width'] = '6000';
        $config['max_height'] = '4500';

        $this->load->library('upload',$config);
        for($i=0; $i<count($_FILES['file']['name']); $i++)
        {
            $_FILES['userfile']['name']= $_FILES['file']['name'][$i];
            $_FILES['userfile']['type']= $_FILES['file']['type'][$i];
            $_FILES['userfile']['tmp_name']= $_FILES['file']['tmp_name'][$i];
            $_FILES['userfile']['error']= $_FILES['file']['error'][$i];
            $_FILES['userfile']['size']= $_FILES['file']['size'][$i];

            if(! $this->upload->do_upload())
            {
                /*----set flash message*/
                echo "error";

            }
            else
            {
                echo "done";

            }

        }
}

解决方法:

尝试使用这样,简单易行

    $("#uploadBusinessImg").on("click",function(e)
    {

               var formData = new FormData($("#form_name")[0]);
                $.ajax({
                    url: '<?PHP echo site_url('Main_ctrl/upload_business_photo_do'); ?>',
                    processData: false,
                    contentType: false,
                    data: formData,
                    type: 'POST', async : true,
                    success: function(data){
                        alert(data);
                    }
                });
      });

并在控制器中使用这样的

if($_FILES['txtBusinessImageName']) 
    {
        $file_ary =  $this->reArrayFiles($_FILES['txtBusinessImageName']);

        foreach ($file_ary as $file) 
        {
            print 'File Name: ' . $file['name'];
            print 'File Type: ' . $file['type'];
            print 'File Size: ' . $file['size'];
        }
     }

并且还使用此功能文件数据转换为多个图像数据的数组

function reArrayFiles(&$file_post) {

    $file_ary = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);

    for ($i=0; $i<$file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_ary[$i][$key] = $file_post[$key][$i];
        }
    }

    return $file_ary;
}

它的工作完美,只是尝试使用它.您不需要使用ajax添加额外的文件代码.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐