js動態(tài)創(chuàng)建上傳表單通過iframe模擬Ajax實現(xiàn)無刷新
更新時間:2014年02月20日 09:46:22 作者:
這篇文章主要介紹了js動態(tài)創(chuàng)建上傳表單通過iframe模擬Ajax無刷新的具體實現(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'; 通過iframe 查看后臺數(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 '文件名稱:'.$name.'<br>';
echo '文件類型:'.$type.'<br>';
echo '文件大小:'.round($size/1024).'K<br>';
echo '文件臨時存放路徑:'.$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 '文件名稱'.$img[2];*/
$data['error'] = 0;
exit("<script>parent.callback(".json_encode($data).");</script>");
}elseif($error==1){
$data['error'] = 1;
$data['meg'] = '超過了文件大小,在php.ini文件中設(shè)置';
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>");
}
?>
您可能感興趣的文章:
- ajax(iframe)無刷新提交表單、上傳文件
- Ajax方式提交帶文件上傳的表單及隱藏iframe應(yīng)用
- 詳解Ajax和form+iframe 實現(xiàn)文件上傳的方法(兩種方式)
- iframe實現(xiàn)Ajax文件上傳效果示例
- 原生ajax和iframe框架實現(xiàn)圖片文件上傳的兩種方式
- PHP實現(xiàn)帶進(jìn)度條的Ajax文件上傳功能示例
- PHP使用HTML5 FileApi實現(xiàn)Ajax上傳文件功能示例
- php+ajax 文件上傳代碼實例
- PHP+Ajax實現(xiàn)上傳文件進(jìn)度條動態(tài)顯示進(jìn)度功能
- PHP+iframe模擬Ajax上傳文件功能示例
相關(guān)文章
基于javascript實現(xiàn)漂亮的頁面過渡動畫效果附源碼下載
本文通過javascript實現(xiàn)漂亮的頁面過濾動畫效果,用戶通過點擊頁面左側(cè)的菜單,對應(yīng)的頁面加載時伴隨著滑動過濾動畫,并帶有進(jìn)度條效果。用戶體驗度非常好,感興趣的朋友一起看看吧2015-10-10
JavaScript創(chuàng)建命名空間(namespace)的最簡實現(xiàn)
JavaScript創(chuàng)建命名空間(namespace)通過自定義函數(shù)進(jìn)行類型判斷、數(shù)組遍歷、函數(shù)執(zhí)行等相關(guān)操作來實現(xiàn)命名空間的功能,需要的朋友可以參考一下2007-12-12
javascript新建標(biāo)簽,判斷鍵盤輸入,以及判斷焦點(示例代碼)
這篇文章主要介紹了javascript新建標(biāo)簽,判斷鍵盤輸入,以及判斷焦點(示例代碼)。需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11
javascript中關(guān)于break,continue的特殊用法與介紹
javascript大家所熟知中的for是一個循環(huán)體,循環(huán)體其中的break和continue也是大家都比較熟悉的功能,相信大家對它們的用法不會陌生,本文不是介紹其功能,本文假設(shè)你已經(jīng)熟悉break和continue的語意和用法2012-05-05
Layui彈框中數(shù)據(jù)表格中可雙擊選擇一條數(shù)據(jù)的實現(xiàn)
這篇文章主要介紹了Layui彈框中數(shù)據(jù)表格中可雙擊選擇一條數(shù)據(jù)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

