首页 > web前端 > js教程 > js动态创建上传表单通过iframe模拟Ajax实现无刷新_javascript技巧

js动态创建上传表单通过iframe模拟Ajax实现无刷新_javascript技巧

WBOY
发布: 2016-05-16 16:59:02
原创
836 人浏览过
复制代码 代码如下:

<script> <BR>window.onload=function(){ <BR>upfile('file.php'); <BR>} <br><br><BR>/* <BR>** url 路径 <BR>**/ <BR>function upfile(url){ <BR>//创建iframe <BR>var iframe = document.createElement("iframe"); <BR>document.body.appendChild(iframe); <BR>iframe.id = 'iframeName'; <BR>iframe.name = 'iframeName'; <BR>iframe.style.display = 'none'; <BR>//创建form <BR>var turnForm = document.createElement("form"); <BR>document.body.appendChild(turnForm); <BR>turnForm.method = 'post'; <BR>turnForm.action = url; <BR>turnForm.encoding = "multipart/form-data"; <BR>turnForm.name = 'formNamer'; <BR>turnForm.target = 'iframeName'; <BR>//创建隐藏表单 <BR>var newElement = document.createElement("input"); <BR>newElement.setAttribute("name","inputname"); <BR>newElement.setAttribute("type","file"); <BR>newElement.setAttribute("value",''); <BR>turnForm.appendChild(newElement); <BR>} <br><br>//form提交 <BR>function formSubmit() { <BR>//var res = document.formNamer.inputname.value; <BR>document.formNamer.submit(); <BR>} <br><br>//返回值 <BR>function callback(result){ <BR>//window.parent.document.getElementById('iframeName').style.display = 'block'; 通过iframe 查看后台数据 <BR>if(result['error'] == 0){ <BR>document.getElementById('test').src= result['img'][1]+'/'+result['img'][2]; <BR>}else if(result['error'] == 1){ <BR>alert(result['meg']); <BR>}else if(result['error'] == 2){ <BR>alert(result['meg']); <BR>}else if(result['error'] == 3){ <BR>alert(result['meg']); <BR>}else if(result['error'] == 4){ <BR>alert(result['meg']); <BR>}else{ <BR>alert(result['meg']); <BR>} <br><br>} <br><br></script>

上传
js动态创建上传表单通过iframe模拟Ajax实现无刷新_javascript技巧

file.php
复制代码 代码如下:

header("content-Type: text/html; charset=Utf-8");

if(@is_uploaded_file($_FILES['inputname']['tmp_name'])){

$f = $_FILES['inputname'];
$name = $f["name"];
$size = $f["size"];
$type = $f["type"];
$fileName = $f["tmp_name"];

switch ($type) {
case 'image/jpg':$okType = true;
break;

case 'image/jpeg':$okType = true;
break;

case 'image/png':$okType = true;
break;

case 'image/gif':$okType = true;
break;
}

if($okType){
$error = $f["error"];
echo '文件名称:'.$name.'
';
echo '文件类型:'.$type.'
';
echo '文件大小:'.round($size/1024).'K
';
echo '文件临时存放路径:'.$fileName.'
';

$fileDir = dirname(__FILE__).'/img/upfile'.time().$name;
$img = explode('/',$fileDir);

move_uploaded_file($fileName, $fileDir);

$data = array();
$data['img'] = $img;
$data['name'] = $name;
$data['type'] = $type;
$data['size'] = $size;
$data['filename'] = $fileName;
$data['fileDir'] = $fileDir;

if($error==0){
/*echo '上传成功!';
echo '预览:';
echo "js动态创建上传表单通过iframe模拟Ajax实现无刷新_javascript技巧
";
echo '文件名称'.$img[2];*/
$data['error'] = 0;
exit("<script>parent.callback(".json_encode($data).");</script>");

}elseif($error==1){
$data['error'] = 1;
$data['meg'] = '超过了文件大小,在php.ini文件中设置';
exit("<script>parent.callback(".json_encode($data).");</script>");
}elseif ($error==2){
$data['error'] = 2;
$data['meg'] = '超过了文件的大小MAX_FILE_SIZE选项指定的值';
exit("<script>parent.callback(".json_encode($data).");</script>");
}elseif ($error==3){
$data['error'] = 3;
$data['meg'] = '文件只有部分被上传';
exit("<script>parent.callback(".json_encode($data).");</script>");
}elseif ($error==4){
$data['error'] = 4;
$data['meg'] = '没有文件被上传';
exit("<script>parent.callback(".json_encode($data).");</script>");
}else{
$data['meg'] = '上传文件大小为0';
exit("<script>parent.callback(".json_encode($data).");</script>");
}

}
}else{
$data['error'] = 4;
$data['meg'] = '没有文件被上传';
exit("<script>parent.callback(".json_encode($data).");</script>");
}
?>
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板