时间:2021-07-01 10:21:17 帮助过:5人阅读
- <html>
- <body>
- <h1>Ajax file upload sampleh1><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"
- datadatadata = data + spliter + "rn"
- datadatadata = data + "Content-Disposition: form-data; name="photofile"; filename="C:\a.txt"rn"
- // datadatadata = data + "Content-Type: image/pjpeg" + vbCrLf
- datadatadata = 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>
PHP Ajax服务器端代码: upload.php
- php
- // $_FILES['photofile']:是获得上传图片的数组
- // $uploadfile:存放地址
- $uploadfile = "D:/".$_FILES['photofile']['name'];
- copy( $_FILES['photofile']['tmp_name'], $uploadfile );
- echo "URL: <a href='http://localhost/".$_FILES['photofile']['name']."' target='_blank'>".$_FILES['photofile']['name']."a><br/>";
- ?>
以上所写代码就是基本的PHP Ajax实现无刷新图片上传的具体解决办法。
http://www.bkjia.com/PHPjc/446386.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446386.htmlTechArticle作为一个 PHP Ajax客户端页面代码: index.html html body h1 Ajaxfileuploadsample / h1 br / input id = "uplaod" name = "btn_send" type = "button" value = "上传测试" /...