二款php ajax文件上传代码(1/4)
二款php教程 ajax文件上传代码
这两款ajax文件上传代码其实都是利用了js的iframe或ajax post来实现的下面来看看代码吧。
<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=网页特效>
// 上传函数
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>
<html>
<script language="javascript">
<!--
var xmlhttp;
function createxmlhttprequest() {
if (window.activexobject) {
xmlhttp = new activexobject("microsoft.xmlhttp");
}
else if (window.xmlhttprequest) {
xmlhttp = new xmlhttprequest();
}
}
function uploade(e){
var fileadd
fileadd = e;
createxmlhttprequest();
xmlhttp.onreadystatechange = handlestatechange;
var url = "pic_upload.asp教程?add="+fileadd+"×tamp="+new date().gettime();
xmlhttp.open("get",url,true);
xmlhttp.send(null);
}
function handlestatechange() {
document.getelementbyid("content").innerhtml = "这里写进度条";
if (xmlhttp.readystate == 4) {
if (xmlhttp.status == 200) {
document.getelementbyid("content").innerhtml = xmlhttp.responsetext;
}
else{
//alert(xmlhttp.status);
alert('错误,请联系管理员!');
}
}
}
//-->
</script>
<body>
<input type=file name="mefile" id="fileadd" onchange="alert(document.getelementbyid('fileadd').value);">
<input type="submit" value="上传" onclick="uploade(document.getelementbyid('fileadd').value);">
<div id="content"></div>
</body>
</html>
补充:Php教程,Php高级应用