ajax无刷新上传

form.php
[html] view plain copy
  1. <input type="file" id="file" multiple="true"/>  
  2. <button onclick="upload()">upload</button>  
  3. <script>  
  4.     function upload(){  
  5.         var file = document.getElementById("file").files;  
  6.         var formData = new FormData();  
  7.         formData.append("file",file[0]);  
  8.         var ajax = new XMLHttpRequest();  
  9.         ajax.onreadystatechange=function()  
  10.         {  
  11.             if(ajax.status==200 && ajax.readyState==4){  
  12.                 console.log(ajax.responseText);  
  13.             }  
  14.         }  
  15.         ajax.open("post","xhr.php");  
  16.         ajax.send(formData);  
  17.     }  
  18. </script>  

xhr.php

[html] view plain copy
  1. <?php  
  2.     $name=$_FILES['file']['name'];  
  3.     move_uploaded_file($_FILES['file']['tmp_name'],iconv("UTF-8","gb2312",$name)) 

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