无法在php中读取FormData对象

时间:2016-03-13 11:22:35

标签: javascript php ajax codeigniter form-data

我想在服务器上使用Ajax函数和使用formdata对象上传一个文件。点击按钮上传时,我显示了输入文件的所有值。

  

姓名:" Lich hoc.txt"

     

尺寸:1950

     

键入:" text / plain"

但我的麻烦是我无法在我的php代码中读取对象,var_dump什么都没有。 我使用框架Codeigniter。 请帮我 !感谢

HTML CODE

<form enctype="multipart/form-data" name="" id=""  method="post" action="">
    <input id="file" type="file" name="file" />
    <button class="button" id="btnDangKi"> Đăng Kí </button>
</form>

JS CODE

        $(document).ready(function(){
            $('#btnDangKi').click(function(){
                UpLoadFile();
            });

        });

       function UpLoadFile(){
            var file_upload = document.getElementById("file").files[0];
            var fd = new FormData();
            fd.append("file",file_upload);
            console.log(file_upload);
            $.ajax({
                url:'<?php echo base_url('QuanLyXe_Ajax/upload'); ?>',
                data: fd,
                processData: false,
                contentType: false,
                type: 'POST',
                enctype: 'multipart/form-data',
                mimeType: 'multipart/form-data',
                success: function(data){
                    alert(data);
                }
            });
        }

PHP代码

    public function UpLoad(){
    $config['upload_path'] = './assets/uploads/';
    $config['allowed_types'] = 'gif|jpg|png|txt';

    $this->load->library('upload', $config);

    if ($this->upload->do_upload("file"))
    {
        echo "Thanh cong upload ";
    }else
    {
        echo $this->upload->display_errors();
    }

    echo var_dump($_POST);
}

1 个答案:

答案 0 :(得分:2)

STYLE CODE:)

<style>
#upload_button{

padding:5px 10px;
border:1px solid gray;
border-radius:10px;
cursor:hand;
cursor:pointer;
float:left;
margin-top:5px;
}

#upload_form{

float:left;
margin-right:10px;
width:300px;
padding:10px;

}
    </style>

jQUERY CODE

<script>

  $(document).ready(function(){
            $('#upload_button').click(function(){
                UpLoadFile();
            });

        });

       function UpLoadFile(){
       var formData = new FormData($("#form_file")[0]);
        $.ajax({
            url: "yukle.php",
            type: "POST",
            data: formData,
            async: false,
            success: function (cevap) {
            $("#sonuc").append(cevap);
            },
            cache: false,
            contentType: false,
            processData: false
        });

        e.preventDefault();
        }

</script>

HTML CODE

<div id="upload_form">
<form enctype="multipart/form-data" name="form_file" id="form_file"  method="post" action="">
<input id="file" type="file" name="file" />
</form>
</div>

<div id="upload_button"> Upload</div>

PHP代码

public function UpLoad($file){

if (
$file["type"]=="image/jpeg" or 
$file["type"]=="image/png" or 
$file["type"]=="image/gif"){
$dosya_adi=$file["name"];
$uzanti=substr($dosya_adi,-4,4);
$yeni_ad= './assets/uploads/'.$dosya_adi.$uzanti;
if (move_uploaded_file($file["tmp_name"],$yeni_ad)){
echo 'Files uploaded';
}else{
echo 'Upload Error';
}
}else{
echo 'Error';
}

}

UpLoad($_POST['file']);