iframe页面提交表单后自动关闭iframe并刷新父页面
1个回答
展开全部
给你个参考:
HTML页面:
<html>
<head>
<title>上传文件</title>
<script src="jquery.js"></script>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
<input type="text" name="up" id="up"/><a href="javascript:void(0);">上传</a>
<div style="width:300px;height:400px;display:none;">
<iframe src="upload.php">
</iframe>
</div>
<script>
$(function(){
$("a").click(function(){
$("div").show();
$("iframe").contents().find("#upload").click();
$("iframe").contents().find("input[type!=submit]").val("");
});
});
</script>
</body>
</html>
PHP页面:
<?php
$newfile = "";
$sub = "";
if(!empty($_POST['submit'])=="submit"){
$sub = $_POST['submit'];
if($_FILES['upload']['error']>0){
//echo "ERROR:".$_FILES['upload']['error']."<br/>";
}else{
$upload_dir = "upload";
$filename = $_FILES['upload']['name'];
if(!is_dir($upload_dir)){
if(!mkdir($upload_dir)){
echo "<script>alert('创建目录失败');</script>";
exit;
}
}else{
$str = "";
for($i=0;$i<4;$i++){
$str .=mt_rand(0,9);
}
$farr = explode(".",$filename);
$hz = end($farr);
$newfile = $upload_dir."/".time()."_".$str.".".$hz;
move_uploaded_file($_FILES['upload']["tmp_name"],$newfile);
}
}
}
?>
<html>
<head>
<title>文件上传</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script src="jquery.js"></script>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="upload" id="upload"/><input id="form_sub" type="submit" value="上传" name="submit"/>
</form>
<?php if($newfile!=""){ ?>
<input type="hidden" value="<?php echo $newfile; ?>" id="upfilename"/>
<input type="hidden" value="<?php echo $sub; ?>" id="upsub"/>
<?php } ?>
<script>
$(function(){
var upfilename = $("#upfilename").val();
var upsub = $("#upsub").val();
if((upsub !="")&&(upfilename!="")){
$(window.parent.document).find("#up").val(upfilename);
$(window.parent.document).find("div").hide();
}
$("#upload").change(function(){
var arr_hz = ["jpg",'gif','bmp','jpeg'];
var filename = $("#upload").val();
var hz = filename.split(".");
var hzlth = hz.length;
if(in_array(hz[hzlth-1],arr_hz)){
$("#form_sub").click();
}else{
alert("文件格式不正确!");
$("#upload").val("");
$(window.parent.document).find("#up").val(upfilename);
$(window.parent.document).find("div").hide();
}
});
function in_array(param,arr){
var lth = arr.length;
for(i=0;i<lth;i++){
if(arr[i].toLowerCase()==param.toLowerCase()){
return true;
}
}
return false;
}
})
</script>
</body>
</html>
希望能帮助到你
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询