Heim >Web-Frontend >js-Tutorial >js动态创建上传表单通过iframe模拟Ajax实现无刷新_javascript技巧

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

WBOY
WBOYOriginal
2016-05-16 16:59:02830Durchsuche
复制代码 代码如下:

<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>");
}
?>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn