多文件上传(input标签file)

html页面

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="UTF-8"/>  
  5.         <title>xhr2</title>  
  6.     </head>  
  7.     <body>      
  8.         <div style="text-align:center;margin:100px">        
  9.             <input type="file" id="file" name="file" multiple="multiple">  
  10.             <button onclick="xhr2()">多文件上传</button>  
  11.         </div>          
  12.         <script>  
  13.         function xhr2(){  
  14.             var xhr = new XMLHttpRequest();//第一步  
  15.             //定义表单变量  
  16.             var file = document.getElementById('file').files;  
  17.             //console.log(file.length);  
  18.             //新建一个FormData对象  
  19.             var formData = new FormData(); //++++++++++  
  20.             //追加文件数据  
  21.             for(i=0;i<file.length;i++){    
  22.                  formData.append("file["+i+"]", file[i]); //++++++++++  
  23.             }   
  24.             //formData.append("file", file[0]); //++++++++++  
  25.               
  26.             //post方式  
  27.             xhr.open('POST', 'xhr2.php'); //第二步骤  
  28.             //发送请求  
  29.             xhr.send(formData);  //第三步骤  
  30.             //ajax返回  
  31.             xhr.onreadystatechange = function(){ //第四步  
  32.             if ( xhr.readyState == 4 && xhr.status == 200 ) {  
  33.               console.log( xhr.responseText );  
  34.             }  
  35.           };  
  36.             //设置超时时间  
  37.             xhr.timeout = 100000;  
  38.             xhr.ontimeout = function(event){  
  39.             alert('请求超时!');  
  40.           }   
  41.         }  
  42.         </script>  
  43.     </body>  
  44. </html>  

php处理页面

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. print_r($_FILES["file"]);  
  3.   
  4. for($i=0;$i<count($_FILES["file"]['name']);$i++){  
  5. $name=$_FILES["file"]["name"][$i];  
  6. move_uploaded_file($_FILES["file"]["tmp_name"][$i],iconv("UTF-8","gb2312",$name));  
  7. }  
  8.   
  9. ?>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值