DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX基礎知識 >> php ajax無刷新上傳圖片實例代碼
php ajax無刷新上傳圖片實例代碼
編輯:AJAX基礎知識     
AJAX 客戶端頁面代碼: index.html
復制代碼 代碼如下:
<html>
<body>
<h1>Ajax file upload sample</h1><br/><input id="uplaod" name="btn_send" type="button" value="上傳測試"/>
<div id=result></div>
<PRE class=js name="code"><SCRIPT LANGUAGE=JavaScript>
// 上傳函數
function btn_send.onclick() {
data = ""
spliter = "-------7d8d733180846"
datadata = data + spliter + "\r\n"
datadata = data + "Content-Disposition: form-data; name=\"photofile\"; filename=\"C:\\a.txt\"\r\n"
// datadata = data + "Content-Type: image/pjpeg" + vbCrLf
datadata = data + "Content-Type: text/plain" + "\r\n" + "\r\n"
text = "My name is Wilson Lin."
postLength = text.length + data.length + 2 + spliter.length + 4
package = data + text + "\r\n" + spliter + "--\r\n"

alert(package)
// 把XML文檔發送到Web服務器
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("POST","./upload.php",false);
xmlhttp.setRequestHeader("Content-Type", "multipart/form-data; boundary=-----7d8d733180846");
xmlhttp.setRequestHeader("Content-Length", postLength);
xmlhttp.send(package);
// 顯示服務器返回的信息
result.innerHTML=xmlhttp.ResponseText;
}
</SCRIPT>
</PRE>
</body>
</html>

PHP服務器端代碼: upload.php
復制代碼 代碼如下:
<?php
// $_FILES['photofile']:是獲得上傳圖片的數組
// $uploadfile:存放地址
$uploadfile = "D:/".$_FILES['photofile']['name'];
copy( $_FILES['photofile']['tmp_name'], $uploadfile );
echo "URL: <a href='http://localhost/".$_FILES['photofile']['name']."' target='_blank'>".$_FILES['photofile']['name']."</a><br/>";
?>
Upload successed!
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved