一个文件域实现多文件的上传<一>
2016-07-18 19:53
405 查看
一个文件域实现多文件的上传:表单页面:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title>多文件上传</title> </head> <body> <input type="file" id="file" name="file" multiple="multiple"> <button onclick="xhr2()">多文件上传</button> <script> function xhr2(){ var xhr = new XMLHttpRequest(); // 初始化ajax var file = document.getElementById('file').files; // 定义表单变量 //console.log(file.length); // firbug中调试用 var formData = new FormData(); //新建一个FormData对象
<span style="white-space: pre;"> </span>//追加文件数据
for(i=0;i<file.length;i++){ formData.append("file["+i+"]", file[i]); } //formData.append("file", file[0]); xhr.open('POST', 'files.php'); // 打开一个链接 采用post传值 xhr.send(formData); // 向服务器发送请求 xhr.onreadystatechange = function(){ //第四步 if ( xhr.readyState == 4 && xhr.status == 200 ) { console.log( xhr.responseText ); } }; // 设置超时时间 xhr.timeout = 100000; xhr.ontimeout = function(event){ alert('请求超时!'); } </script> </body> </html>php页面:
<?php header('content-type:text/html;charset=utf-8');/*循环输出图片的路径并实现添加*/for($i=0;$i<count($_FILES['files']['name']);$i++){$path = "/file/".$_FILES['files']['name'][$i]; // 文件路径move_uploaded_file($_FILES['files']['tmp_name'][$i], $path);}
相关文章推荐
- SqlServer Convert函数 日期格式化
- LeetCode 258. Add Digits两种方法
- SLAM 数据集
- 0718
- Java开发平台下如何区分JDK,Tomcat,eclipse的32位与64版本
- 使用Slidingmenu爆出“Both setBehindContentView must be called in onCreate in addition to setContentView.”
- uva11183Teen Girl Squad【最小树形图定根模板题】
- 用pyenv和virtualenv搭建单机多版本python虚拟开发环境-命令
- C++ 语法 1. struct 内存的对齐原则
- sql之left join、right join、inner join的区别
- 分布式一致性协议学习笔记(一)--Paxos
- 《AngularJS》------自定义服务 provider、service、factory
- Dialog的回退按钮的点击事件
- jquery动画显示效果
- kafka的相关操作脚本
- Lwip学习笔记之UDP
- 身份验证中Cookies与 Tokens比较
- 关于kubernetes 的 cluster IP , node IP ,container IP , 和 VIP
- 对map集合进行排序
- C/C++笔记(C语言重要问题重讲,数据篇)