js動(dòng)態(tài)創(chuàng)建上傳表單通過(guò)iframe模擬Ajax實(shí)現(xiàn)無(wú)刷新
更新時(shí)間:2014年02月20日 09:46:22 作者:
這篇文章主要介紹了js動(dòng)態(tài)創(chuàng)建上傳表單通過(guò)iframe模擬Ajax無(wú)刷新的具體實(shí)現(xiàn),需要的朋友可以參考下
復(fù)制代碼 代碼如下:
<script>
window.onload=function(){
upfile('file.php');
}
/*
** url 路徑
**/
function upfile(url){
//創(chuàng)建iframe
var iframe = document.createElement("iframe");
document.body.appendChild(iframe);
iframe.id = 'iframeName';
iframe.name = 'iframeName';
iframe.style.display = 'none';
//創(chuàng)建form
var turnForm = document.createElement("form");
document.body.appendChild(turnForm);
turnForm.method = 'post';
turnForm.action = url;
turnForm.encoding = "multipart/form-data";
turnForm.name = 'formNamer';
turnForm.target = 'iframeName';
//創(chuàng)建隱藏表單
var newElement = document.createElement("input");
newElement.setAttribute("name","inputname");
newElement.setAttribute("type","file");
newElement.setAttribute("value",'');
turnForm.appendChild(newElement);
}
//form提交
function formSubmit() {
//var res = document.formNamer.inputname.value;
document.formNamer.submit();
}
//返回值
function callback(result){
//window.parent.document.getElementById('iframeName').style.display = 'block'; 通過(guò)iframe 查看后臺(tái)數(shù)據(jù)
if(result['error'] == 0){
document.getElementById('test').src= result['img'][1]+'/'+result['img'][2];
}else if(result['error'] == 1){
alert(result['meg']);
}else if(result['error'] == 2){
alert(result['meg']);
}else if(result['error'] == 3){
alert(result['meg']);
}else if(result['error'] == 4){
alert(result['meg']);
}else{
alert(result['meg']);
}
}
</script>
<a href="javascript:formSubmit()">上傳</a>
<img src="http://blog.163.com/zhwxl_zyx/blog/img/pasic.jpg" id="test" width="200" height="200">
file.php
復(fù)制代碼 代碼如下:
<?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 '文件名稱(chēng):'.$name.'<br>';
echo '文件類(lèi)型:'.$type.'<br>';
echo '文件大小:'.round($size/1024).'K<br>';
echo '文件臨時(shí)存放路徑:'.$fileName.'<br>';
$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 '預(yù)覽:';
echo "<img src="http://blog.163.com/zhwxl_zyx/blog/.$img[1].'/'.$img[2]." width='200' height='200'><br>";
echo '文件名稱(chēng)'.$img[2];*/
$data['error'] = 0;
exit("<script>parent.callback(".json_encode($data).");</script>");
}elseif($error==1){
$data['error'] = 1;
$data['meg'] = '超過(guò)了文件大小,在php.ini文件中設(shè)置';
exit("<script>parent.callback(".json_encode($data).");</script>");
}elseif ($error==2){
$data['error'] = 2;
$data['meg'] = '超過(guò)了文件的大小MAX_FILE_SIZE選項(xiàng)指定的值';
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'] = '沒(méi)有文件被上傳';
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'] = '沒(méi)有文件被上傳';
exit("<script>parent.callback(".json_encode($data).");</script>");
}
?>
您可能感興趣的文章:
- ajax(iframe)無(wú)刷新提交表單、上傳文件
- Ajax方式提交帶文件上傳的表單及隱藏iframe應(yīng)用
- 詳解Ajax和form+iframe 實(shí)現(xiàn)文件上傳的方法(兩種方式)
- iframe實(shí)現(xiàn)Ajax文件上傳效果示例
- 原生ajax和iframe框架實(shí)現(xiàn)圖片文件上傳的兩種方式
- PHP實(shí)現(xiàn)帶進(jìn)度條的Ajax文件上傳功能示例
- PHP使用HTML5 FileApi實(shí)現(xiàn)Ajax上傳文件功能示例
- php+ajax 文件上傳代碼實(shí)例
- PHP+Ajax實(shí)現(xiàn)上傳文件進(jìn)度條動(dòng)態(tài)顯示進(jìn)度功能
- PHP+iframe模擬Ajax上傳文件功能示例
相關(guān)文章
基于javascript實(shí)現(xiàn)漂亮的頁(yè)面過(guò)渡動(dòng)畫(huà)效果附源碼下載
本文通過(guò)javascript實(shí)現(xiàn)漂亮的頁(yè)面過(guò)濾動(dòng)畫(huà)效果,用戶(hù)通過(guò)點(diǎn)擊頁(yè)面左側(cè)的菜單,對(duì)應(yīng)的頁(yè)面加載時(shí)伴隨著滑動(dòng)過(guò)濾動(dòng)畫(huà),并帶有進(jìn)度條效果。用戶(hù)體驗(yàn)度非常好,感興趣的朋友一起看看吧2015-10-10
JavaScript創(chuàng)建命名空間(namespace)的最簡(jiǎn)實(shí)現(xiàn)
JavaScript創(chuàng)建命名空間(namespace)通過(guò)自定義函數(shù)進(jìn)行類(lèi)型判斷、數(shù)組遍歷、函數(shù)執(zhí)行等相關(guān)操作來(lái)實(shí)現(xiàn)命名空間的功能,需要的朋友可以參考一下2007-12-12
javascript新建標(biāo)簽,判斷鍵盤(pán)輸入,以及判斷焦點(diǎn)(示例代碼)
這篇文章主要介紹了javascript新建標(biāo)簽,判斷鍵盤(pán)輸入,以及判斷焦點(diǎn)(示例代碼)。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-11-11
javascript中關(guān)于break,continue的特殊用法與介紹
javascript大家所熟知中的for是一個(gè)循環(huán)體,循環(huán)體其中的break和continue也是大家都比較熟悉的功能,相信大家對(duì)它們的用法不會(huì)陌生,本文不是介紹其功能,本文假設(shè)你已經(jīng)熟悉break和continue的語(yǔ)意和用法2012-05-05
js動(dòng)畫(huà)效果制件讓圖片組成動(dòng)畫(huà)代碼分享
這篇文章主要介紹了使用js讓圖片組成動(dòng)畫(huà),這樣就可以用js做動(dòng)畫(huà)了,大家參考使用吧2014-01-01
Layui彈框中數(shù)據(jù)表格中可雙擊選擇一條數(shù)據(jù)的實(shí)現(xiàn)
這篇文章主要介紹了Layui彈框中數(shù)據(jù)表格中可雙擊選擇一條數(shù)據(jù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

