js连系php完成下载性能
效劳端
步骤就是,设置头文件参数,而后读入并输入文件。上面代码的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> <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的形式。能够应用readfile函数替代,效率更高。
以上就是js php完成无刷新下载性能的具体内容,更多请存眷资源魔其它相干文章!
标签: php js php开发教程 php开发资料 php开发自学 下载