国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP Ajax文件異步上傳代碼(XMLHttpRequest)例子

2024-05-04 21:49:55
字體:
來源:轉載
供稿:網友

文件異步上傳通常是通過ajax實現了,也有朋友說使用iframe來實現這個雖然說可以達到目的但不是真正的a異步上傳了并且如果瀏覽器不支持irame那么iframe模仿上傳就無效了,下面我們來看一段真正的文件異步上傳例子.

PHP 代碼:

  1. $fileName = $_FILES['afile']['name']; 
  2. $fileType = $_FILES['afile']['type']; 
  3. $fileContent = file_get_contents($_FILES['afile']['tmp_name']); 
  4. $dataUrl = 'data:' . $fileType . ';base64,' . base64_encode($fileContent); 
  5. $json = json_encode(array
  6.   'name' => $fileName
  7.   'type' => $fileType
  8.   'dataUrl' => $dataUrl
  9.   'username' => $_REQUEST['username'], 
  10.   'accountnum' => $_REQUEST['accountnum'
  11. )); 
  12. echo $json
  13.  
  14. Html 及 JS 代碼 
  15. <!DOCTYPE html> 
  16. <!-- 
  17. Copyright 2012 Google Inc. 
  18. Licensed under the Apache License, Version 2.0 (the "License"); 
  19. you may not use this file except in compliance with the License. 
  20. You may obtain a copy of the License at 
  21.      http://www.apache.org/licenses/LICENSE-2.0 
  22. Unless required by applicable law or agreed to in writing, software 
  23. distributed under the License is distributed on an "AS IS" BASIS, 
  24. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  25. See the License for the specific language governing permissions and 
  26. limitations under the License. 
  27. Author: Eric Bidelman (ericbidelman@chromium.org) 
  28. --> 
  29. <html> 
  30. <head> 
  31. <meta charset="utf-8" /> 
  32. <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" /> 
  33. <title>xhr.send(FormData) Example</title> 
  34. </head> 
  35. <body> 
  36.  
  37. <input type="file" name="afile" id="afile" accept="image/*"/> 
  38.  
  39. <script> 
  40. document.querySelector('#afile').addEventListener('change'function(e) { 
  41.   var file = this.files[0]; 
  42.   var fd = new FormData(); 
  43.   fd.append("afile", file); 
  44.   // These extra params aren't necessary but show that you can include other data. 
  45.   fd.append("username""Groucho"); 
  46.   fd.append("accountnum", 123456); 
  47.   var xhr = new XMLHttpRequest(); 
  48.   xhr.open('POST''handle_file_upload.php', true); 
  49.    
  50.   xhr.upload.onprogress = function(e) { 
  51.     if (e.lengthComputable) { 
  52.       var percentComplete = (e.loaded / e.total) * 100; 
  53.       console.log(percentComplete + '% uploaded'); 
  54.     } 
  55.   }; 
  56.   xhr.onload = function() { 
  57.     if (this.status == 200) { 
  58.       var resp = JSON.parse(this.response); 
  59.       console.log('Server got:', resp); 
  60.       var image = document.createElement('img'); 
  61.       image.src = resp.dataUrl; 
  62.       document.body.appendChild(image); 
  63.     }; 
  64.   }; 
  65.   xhr.send(fd); 
  66. }, false); 
  67. </script> 
  68. <!--[if IE]> 
  69. <script src="http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js"></script> 
  70. <script>CFInstall.check({mode: 'overlay'});</script> 
  71. <![endif]--> 
  72. </body> 
  73. </html>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沧州市| 阳朔县| 新郑市| 江山市| 克什克腾旗| 铁力市| 阜阳市| 通许县| 扶绥县| 布拖县| 巍山| 通城县| 镇江市| 隆尧县| 泰和县| 茶陵县| 乳源| 石景山区| 清丰县| 衡东县| 鄄城县| 克拉玛依市| 错那县| 高雄市| 景德镇市| 彭阳县| 五家渠市| 安宁市| 嫩江县| 日照市| 南昌县| 同心县| 白银市| 陵水| 盖州市| 乐清市| 新龙县| 日照市| 嘉定区| 乐都县| 宁国市|