<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>iframe上传文件</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="自学IT网 http://www.zixue.it" />
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
function ajaxup()
{
var ifame = 'up' + Math.random();
$('<iframe name="'+ifame +'" width="0" height="0" frameBorder="0"></iframe>').appendTo($('body'));
$('form:first').attr('target',ifame);
$('#progress').html('<img src="./loading.gif"/>');
}
</script>
<style type="text/css">
</style>
</head>
<body>
<h1>iframe模拟Ajax文件上传效果</h1>
<h2></h2>
<div id="progress"></div>
<form action="up.php" method="post" enctype="multipart/form-data" οnsubmit="return ajaxup();">
<p>
<input type="file" name="pic"/>
</p>
<p>
<input type="submit" value="提交"/>
</p>
</form>
</body>
</html>
使用到的up.php内容如下:
<?php
sleep(3);
if (empty($_FILES)) {
exit('no file');
}
$error = $_FILES['pic']['error']==0?'succ':'fail';
echo "<script>parent.document.getElementById('progress').innerHTML = '$error'</script>";
//echo "<script>parent.document.getElementsByTagName('h2')[0].innerHTML = '$error'</script>";
?>