说明:将index.html和upload.php文件保存到支持php的空间的同级目录。
1、前台上传页面 index.html
<html>
<body>
<h1>ajax无刷新上传</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 + "rn"
datadata = data + "Content-Disposition: form-data; name="photofile"; filename="C:a.txt"rn"
// datadata = data + "Content-Type: image/pjpeg" + vbCrLf
datadata = data + "Content-Type: text/plain" + "rn" + "rn"
text = "My name is Wilson Lin."
postLength = text.length + data.length + 2 + spliter.length + 4
package = data + text + "rn" + spliter + "--rn"
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>
2、后台接收上传数据的文件 upload.php