將index.Html和upload.PHP文件保存到apache工作目錄,例子使用安裝目錄D:/Program Files/Apache Software Foundation/apache2.2/htdocs/
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