js iframe實現ajax無重新整理檔案上傳例子

來源:互聯網
上載者:User

js代碼如下

 代碼如下 複製代碼

<!doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd">
<html xmlns="http://www.111cn.net /1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>input type file test - </title>
<style>
#uploadImg{
margin:50px auto;
font-size:12px;
position:relative;
font-size:16px;
width:64px;
height:22px;
line-height:22px;
overflow:hidden;
}
#file{
position:absolute;
left:0;
top:0;
height:22px;
width:64px;
opacity: 0;    /* Firefox, Safari(WebKit), Opera */ 
    -ms-filter: "alpha(opacity=0)";     /* IE 8 */ 
    filter: alpha(opacity=0);     /* IE 4-7 */
}
</style>
</head>
<body>
<iframe name="hide_iframe" style="display:none;" src="./test.html"></iframe>
<form action="./test.php" enctype="multipart/form-data" method="POST" target="hide_iframe">
<div id="uploadImg">
<input type="file" id="file" name="file" />
<input type="hidden" name="MAX_FILE_SIZE" value="8388608" />
<a href="#">上傳圖片</a>
</div>
<div id="add"></div>
<div style="margin:0 auto; width:64px;"><input type="submit" value="提交" /></div>
</form>
</body>
</html>
<script type="text/javascript">
var add = function(text){
document.getElementById('add').innerHTML = text;
}
</script>

php 代碼

 代碼如下 複製代碼

<?php
header("Content-type:text/html;charset=utf-8");
$up_file = $_FILES['img'];
$file_name = $up_file['name'];
//print_r($file_name);
if(move_uploaded_file($up_file['tmp_name'],iconv("utf-8","gb2312",$file_name))){
echo "<script>alert('上傳成功'); </script>";
}else{
echo "<script> parent.ajax_upload(''); </script>";
}
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.