欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php ajax无刷新上传图片实例代码

程序员文章站 2022-07-05 19:30:30
ajax 客户端页面代码: index.html 复制代码 代码如下:

ajax file uploa...

ajax 客户端页面代码: index.html
复制代码 代码如下:

<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=javascript>
// 上传函数
function btn_send.onclick() {
data = ""
spliter = "-------7d8d733180846"
datadata = data + spliter + "\r\n"
datadata = data + "content-disposition: form-data; name=\"photofile\"; filename=\"c:\\a.txt\"\r\n"
// datadata = data + "content-type: image/pjpeg" + vbcrlf
datadata = data + "content-type: text/plain" + "\r\n" + "\r\n"
text = "my name is wilson lin."
postlength = text.length + data.length + 2 + spliter.length + 4
package = data + text + "\r\n" + spliter + "--\r\n"

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服务器端代码: 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/>";
?>
upload successed!