问题描述:

There is a link on click of which a file gets downloaded.

On Client's Machine:

1) On some systems; The IE or Firefox have plugin to display PDF inside browser; on click of link.

2) On some systems, The IE do not have plugin; 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. Is it possible?

Query 2 (Important):

Can we download file; 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:

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:

$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);
相关阅读:
Top