将indexhtml和uploadphp文件保存到支持php的空间的同级目录测试运行即可
AJAX 客户端页面代码 indexhtml
<html>
<body>
<h>Ajax file upload sample</h><br/><input id="uplaod" name="btn_send" type="button" value="上传测试"/>
<div id=result></div>
<PRE class=js name="code"><SCRIPT LANGUAGE=JavaScript>
// 上传函数
function btn_sendonclick() {
data = ""
spliter = "dd"
datadata = data + spliter + "rn"
datadata = data + "ContentDisposition: formdata; name="photofile"; filename="C:atxt"rn"
// datadata = data + "ContentType: image/pjpeg" + vbCrLf
datadata = data + "ContentType: text/plain" + "rn" + "rn"
text = "My name is Wilson Lin"
postLength = textlength + datalength + + spliterlength +
package = data + text + "rn" + spliter + "rn"
alert(package)
// 把XML文档发送到Web服务器
var xmlhttp = new ActiveXObject("MicrosoftXMLHTTP");
xml("POST""/uploadphp"false);
xml("ContentType" "multipart/formdata; boundary=dd");
xml("ContentLength" postLength);
xml(package);
// 显示服务器返回的信息
resultinnerHTML=xml
}
</SCRIPT>
</PRE>
</body>
</html> PHP服务器端代码 uploadphp
<?php
// $_FILES[photofile]:是获得上传图片的数组
// $uploadfile存放地址
$uploadfile = "D:/"$_FILES[photofile][name];
copy( $_FILES[photofile][tmp_name] $uploadfile );
echo "URL: <a http://localhost/"$_FILES[photofile][name]" target=_blank>"$_FILES[photofile][name]"</a><br/>";
?>
Upload successed!