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

首頁(yè) > 語(yǔ)言 > PHP > 正文

php+js實(shí)現(xiàn)的無(wú)刷新下載文件功能示例

2024-05-05 00:11:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了php+js實(shí)現(xiàn)的無(wú)刷新下載文件功能。分享給大家供大家參考,具體如下:

服務(wù)器端頁(yè)面

步驟就是,設(shè)置頭文件參數(shù),然后讀入并輸出文件。下面代碼的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);

前端頁(yè)面

在很多時(shí)候,我們下載文件的操作,都是在前端頁(yè)面直接點(diǎn)擊下載的,而不是專門(mén)跳轉(zhuǎn)到上面的download.php去下載。

所以我們需要在前端實(shí)現(xiàn)無(wú)刷新訪問(wèn)download.php來(lái)下載文件,通過(guò)隱藏的iframe來(lái)實(shí)現(xiàn)是不錯(cuò)的方式。下面是代碼:

<!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" 測(cè)試文件.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的方式。可以使用readfile函數(shù)代替,效率更高。

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 德安县| 从江县| 临海市| 新郑市| 达孜县| 宁蒗| 普格县| 香河县| 句容市| 武冈市| 龙川县| 北流市| 西充县| 观塘区| 斗六市| 成都市| 政和县| 通化县| 孙吴县| 攀枝花市| 平遥县| 郧西县| 黑山县| 大理市| 含山县| 威远县| 玉龙| 高邮市| 吉安县| 建阳市| 榆树市| 黄大仙区| 梅河口市| 兰考县| 馆陶县| 皋兰县| 镶黄旗| 黑水县| 房产| 黄石市| 万盛区|