通过Ajax上传图像时出错 [英] getting error while uploading image via Ajax

查看:45
本文介绍了通过Ajax上传图像时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

im having trouble in uploading a multiple file by ajax . here is my code.

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']的名称

When im calling upload_business_photo_do() function via Ajax then it does't able to recive the name of image $_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";

            }

        }
}

推荐答案

尝试像这样使用,它简单又容易

try to use like this , its simple and easy

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

并在控制器中像这样使用

and in controller use like this

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'];
        }
     }

,还可以使用此功能将文件数据转换为多个图像数据的数组

and also use this function for convert files data into array for multiple images data

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添加额外的文件代码.

its working perfect , just try to use it . you don't need to add a extra codes of files with ajax.

这篇关于通过Ajax上传图像时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆