input 选择多个文件上传
2016-07-15 18:47
363 查看
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>xhr2</title>
</head>
<body>
<div style="text-align:center;margin:100px">
<input type="file" id="file" name="file" multiple="multiple">
<button onclick="xhr2()">多文件上传</button>
</div>
<script>
function xhr2(){
var xhr = new XMLHttpRequest();//第一步
//定义表单变量
var file = document.getElementById('file').files;
//console.log(file.length);
//新建一个FormData对象
var formData = new FormData(); //++++++++++
//追加文件数据
for(i=0;i<file.length;i++){
formData.append("file["+i+"]", file[i]); //++++++++++
}
//formData.append("file", file[0]); //++++++++++
//post方式
xhr.open('POST', 'xhr2.php'); //第二步骤
//发送请求
xhr.send(formData); //第三步骤
//ajax返回
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]
view plain
copy
<?php
print_r($_FILES["file"]);
for($i=0;$i<count($_FILES["file"]['name']);$i++){
$name=$_FILES["file"]["name"][$i];
move_uploaded_file($_FILES["file"]["tmp_name"][$i],iconv("UTF-8","gb2312",$name));
}
?>
<html>
<head>
<meta charset="UTF-8"/>
<title>xhr2</title>
</head>
<body>
<div style="text-align:center;margin:100px">
<input type="file" id="file" name="file" multiple="multiple">
<button onclick="xhr2()">多文件上传</button>
</div>
<script>
function xhr2(){
var xhr = new XMLHttpRequest();//第一步
//定义表单变量
var file = document.getElementById('file').files;
//console.log(file.length);
//新建一个FormData对象
var formData = new FormData(); //++++++++++
//追加文件数据
for(i=0;i<file.length;i++){
formData.append("file["+i+"]", file[i]); //++++++++++
}
//formData.append("file", file[0]); //++++++++++
//post方式
xhr.open('POST', 'xhr2.php'); //第二步骤
//发送请求
xhr.send(formData); //第三步骤
//ajax返回
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]
view plain
copy
<?php
print_r($_FILES["file"]);
for($i=0;$i<count($_FILES["file"]['name']);$i++){
$name=$_FILES["file"]["name"][$i];
move_uploaded_file($_FILES["file"]["tmp_name"][$i],iconv("UTF-8","gb2312",$name));
}
?>
相关文章推荐
- 【慢速学数据结构】散列篇
- NLog的配置和使用(一)
- 零散知识学习
- [输入输出 字符串] cf 697B Barnicle
- Android surfaceview详解
- javax.servlet.Filter
- Activity的创建过程分析
- excel 导出
- 基于Word文档的试卷单题拆分工具
- 在线任意进制转换工具 - aTool在线工具
- 一个简单的生产者与消费者的多线程例子(一):简单的框架
- Spring学习总结(14)——Spring10种常见异常解决方法
- Spring学习总结(14)——Spring10种常见异常解决方法
- ajax 小文件上传
- struts2的自动封装
- 在Xshell中使用sz、rz进行本机和远程主机(或虚拟机)之间文件的传输
- org.eclipse.ui.trim.status显示不完整的问题
- thinkphp 缓存的灵活使用
- 第五章 多个消费者监听同一个队列
- php 发送 email