欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php+js實現的無刷新下載文件功能示例

 更新時間:2019年08月23日 09:35:52   作者:koastal  
這篇文章主要介紹了php+js實現的無刷新下載文件功能,結合實例形式分析了php無刷新下載文件的相關原理、實現方法及操作注意事項,需要的朋友可以參考下

本文實例講述了php+js實現的無刷新下載文件功能。分享給大家供大家參考,具體如下:

服務器端頁面

步驟就是,設置頭文件參數,然后讀入并輸出文件。下面代碼的file_get_contents可以使用fread,fclose代替。

download.php

<?php
$filename = $_GET['filename'];
$path = __DIR__."/file/".$filename;
Header( "Content-type: application/octet-stream");
Header( "Accept-Ranges: bytes ");
Header( "Accept-Length: " .filesize($filename));
header( "Content-Disposition: attachment; filename={$filename}");
echo file_get_contents($filename);

前端頁面

在很多時候,我們下載文件的操作,都是在前端頁面直接點擊下載的,而不是專門跳轉到上面的download.php去下載。

所以我們需要在前端實現無刷新訪問download.php來下載文件,通過隱藏的iframe來實現是不錯的方式。下面是代碼:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<a href="javascript:download_file('http://localhost/download.php?filename=\" rel="external nofollow" 測試文件.doc\"')">下載</a>
<script type="text/javascript">
  function download_file(url)
  {
    if (typeof (download_file.iframe) == "undefined")
    {
      var iframe = document.createElement("iframe");
      download_file.iframe = iframe;
      document.body.appendChild(download_file.iframe);
    }
    //alert(download_file.iframe);
    download_file.iframe.src = url;
    download_file.iframe.style.display = "none";
  }
</script>
</body>
</html>

file_get_contents先讀取,然后echo的方式??梢允褂胷eadfile函數代替,效率更高。

更多關于PHP相關內容感興趣的讀者可查看本站專題:《php文件操作總結》、《PHP目錄操作技巧匯總》、《PHP常用遍歷算法與技巧總結》、《PHP數據結構與算法教程》、《php程序設計算法總結》及《PHP網絡編程技巧總結

希望本文所述對大家PHP程序設計有所幫助。

相關文章

最新評論