当前位置:Gxlcms > PHP教程 > 如何运用PHPAjax实现图片的无刷新上传_PHP教程

如何运用PHPAjax实现图片的无刷新上传_PHP教程

时间:2021-07-01 10:21:17 帮助过:5人阅读

作为一个PHP Ajax客户端页面代码: index.html

  1. <html>
  2. <body>
  3. <h1>Ajax file upload sampleh1><br/><input id="uplaod" name="btn_send" type="button" value="上传测试"/>
  4. <div id=result>div>
  5. <PRE class=js name="code"><SCRIPT LANGUAGE=JavaScript>
  6. // 上传函数
  7. function btn_send.onclick() {
  8. data = ""
  9. spliter = "-------7d8d733180846"
  10. datadatadata = data + spliter + "rn"
  11. datadatadata = data + "Content-Disposition: form-data; name="photofile"; filename="C:\a.txt"rn"
  12. // datadatadata = data + "Content-Type: image/pjpeg" + vbCrLf
  13. datadatadata = data + "Content-Type: text/plain" + "rn" + "rn"
  14. text = "My name is Wilson Lin."
  15. postLength = text.length + data.length + 2 + spliter.length + 4
  16. package = data + text + "rn" + spliter + "--rn"
  17. alert(package)
  18. // 把XML文档发送到Web服务器
  19. var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  20. xmlhttp.open("POST","./upload.php",false);
  21. xmlhttp.setRequestHeader("Content-Type", "multipart/form-data; boundary=-----7d8d733180846");
  22. xmlhttp.setRequestHeader("Content-Length", postLength);
  23. xmlhttp.send(package);
  24. // 显示服务器返回的信息
  25. result.innerHTML=xmlhttp.ResponseText;
  26. }
  27. SCRIPT>
  28. PRE>
  29. body>
  30. html>

PHP Ajax服务器端代码: upload.php

  1. php
  2. // $_FILES['photofile']:是获得上传图片的数组
  3. // $uploadfile:存放地址
  4. $uploadfile = "D:/".$_FILES['photofile']['name'];
  5. copy( $_FILES['photofile']['tmp_name'], $uploadfile );
  6. echo "URL: <a href='http://localhost/".$_FILES['photofile']['name']."' target='_blank'>".$_FILES['photofile']['name']."a><br/>";
  7. ?>

以上所写代码就是基本的PHP Ajax实现无刷新图片上传的具体解决办法。


www.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 = "上传测试" /...

人气教程排行