二款php ajax文件上傳代碼這兩款ajax文件上傳代碼其實都是利用了js的iframe或ajax post來實現的下面來看看代碼吧。
二款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>
1 2 3 4