<script type="text/javascript">
window.onload = function(){
document.getElementById('btn').onclick = function(){
var frm = document.getElementById('frm');
var fd = new FormData(frm);
var request = new XmlHttpRequest();
request.open('post', './demo.php');
request.onreadystatechange = function(){
if (request.readystate == 4 && request.status == 200) {
if(request.responseText == '1'){
alert('上传成功!');
}else{
alert('上传失败!');
}
}
}
request.send(fd);
}
}
</script>
</head>
<body>
<form id="frm">
用户名:<input type="text" name="username"><br>
头像:<input type="file" name="face"><br>
<input type="button" value="上传" id="btn">
</form>
</body>
PHP代码:
<?php
$path = './Uploads/'.$_FILES['face']['name'];
echo move_uploaded_file($_FILES['face']['tmp_name'], $path);
?>