ajax无刷新上传

form.php
<input type="file" id="file" multiple="true"/>
<button onclick="upload()">upload</button>
<script>
    function upload(){
        var file = document.getElementById("file").files;
        var formData = new FormData();
        formData.append("file",file[0]);
        var ajax = new XMLHttpRequest();
        ajax.onreadystatechange=function()
        {
            if(ajax.status==200 && ajax.readyState==4){
                console.log(ajax.responseText);
            }
        }
        ajax.open("post","xhr.php");
        ajax.send(formData);
    }
</script>

xhr.php

<?php
    $name=$_FILES['file']['name'];
    move_uploaded_file($_FILES['file']['tmp_name'],iconv("UTF-8","gb2312",$name))

你可能感兴趣的:(ajax无刷新上传)