如何运用PHP Ajax实现图片的无刷新上传
作为一个PHP程序员来说,他们肯定知道如何编写图片上传的代码。但是,基于无刷新的图片上传的代码可能会上手比较难。今天我们就为大家介绍如何运用PHP Ajax来实现这一功能。
PHP 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" datadatadata = data + spliter + "\r\n" datadatadata = data + "Content-Disposition: form-data; name=\"photofile\"; filename=\"C:\\a.txt\"\r\n" // datadatadata = data + "Content-Type: image/pjpeg" + vbCrLf datadatadata = 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 Ajax服务器端代码: upload.php
相关推荐
坚持着执着 2020-07-16
坚持着执着 2020-06-14
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
时光如瑾雨微凉 2020-07-19
83510998 2020-07-18
jiaguoquan00 2020-07-07
李永毅 2020-07-05