简体   繁体   English

HTML / JQuery:如何打开文件; 不刷新新标签; 并保留当前页面状态?

[英]HTML/JQuery: How to open a file; without flashing new tab; and retaining current page state?

There is a link on click of which a file gets downloaded. 单击链接可以下载文件。

On Client's Machine: 在客户的机器上:

1) On some systems; 1)在某些系统上; The IE or Firefox have plugin to display PDF inside browser; IE或Firefox具有用于在浏览器中显示PDF的插件; on click of link. 点击链接。

2) On some systems, The IE do not have plugin; 2)在某些系统上,IE没有插件; so when we click link; 因此,当我们单击链接时; a new tab flash for a second and file get downloaded with option dialog of save/save as open etc. 一个新的选项卡闪烁一秒钟,然后使用保存/保存为打开等选项对话框下载文件。

Following is the way link is getting opened; 以下是链接的打开方式; so that the state of page should be retained: 因此应保留页面状态:

<a href="http://www.gasl.org/refbib/Bible_King_James_Version.pdf" target="_blank">Holy Bible</a>

Now Requirements : 现在要求

Query 1: Client do not want to remove plugin from browser and want to get file downloaded with dialog. 查询1:客户端不想从浏览器中删除插件,而希望通过对话框下载文件。 Is it possible? 可能吗?

Query 2 (Important): Can we download file; 查询2(重要):我们可以下载文件吗? so that new tab does not get flash and file gets downloaded? 这样新的标签页不会被刷新并下载文件?

Update : Can not remove target attribute as if i will remove that and if plugin is still there in browsers then the page state will not be retained at all. 更新 :无法删除目标属性,好像我将删除目标属性,并且如果浏览器中仍存在插件,则页面状态将完全不会保留。

PHP: PHP:

In your link, set the href to "download.php?file=file.pdf or any ID from a database, or just the file name, whatever. Create a download.php file with: 在您的链接中,将href设置为“ download.php?file = file.pdf或数据库中的任何ID,或者只是文件名,无论如何。使用以下方法创建一个download.php文件:

$path = path/of/the/file/; 
$filename = $_GET['file'];
header('Content-Transfer-Encoding: binary');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
header('Accept-Ranges: bytes');
header('Content-Length: ' . filesize($path));
header('Content-Encoding: none');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=' . $filename);
readfile($path);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM