Blogger Information
Blog 34
fans 0
comment 1
visits 24219
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【4.19】PHP作业-文件上传
51靓号网-专注QQ靓号十年精品
Original
736 people have browsed it

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" >
<meta name="description" content=""/>
<meta name="keywords" content=""/>
<title>ajax上传文件</title>
<script type="text/javascript">
//加载提交事件给from设置提交事件
window.onload=function(){

var fm=document.getElementsByTagName('form')[0];

fm.onsubmit=function(evt){
//1、收集信息--普通表单信息和文件信息
var fd=new FormData(fm);//代表事件的元素节点对象

//2、ajax传递表单信息到服务器
var xhr=new XMLHttpRequest();//传递数据
xhr.onreadystatechange=function(){
//判断
if(xhr.readyState==4){
alert(xhr.responseText);//打印传输的信息
}
}
xhr.open('POST','./02.php');//数据传输格式 ,服务器端
xhr.send(fd);//发送

//阻止事件流
evt.preventDefault();//阻止浏览器跳转

}

}

</script>
</head>
<body>
<h2 align="center">用ajax实现上传文件</h2>

<form action="./02.php" method="POST" enctype="multipart/form-data">
	<fieldset>
		<legend align="center">文件上传</legend>
		<p><strong>用户姓名:</strong><input type="text" name="username"/></p>
		<p><strong>选择文件:</strong><input type="file" name="upload"></p>
	</fieldset>	
	<p align="center"><button type="submit" name="submit" >上传</button></p>
</form>

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post