开发者

HTML: force PDF download

I've a link to a PDF file in my page.

Safari opens the pdf file directly in the website. I would like to download it instead.

I've tried to add target="_blank" to <a> element, but it 开发者_如何学Cdoesn't work if the pop-ups are disabled in the browser settings.

How can I solve this?

thanks


To make so, you need to change headers for .PDF files.

So, in your .htaccess, you need to do like this:

AddType application/octet-stream .pdf


You need to set the Content-Disposition HTTP header to attachment for this file.

Content-Disposition: attachment; filename=test.pdf

Depending on the web server you are using the way to configure this might vary. Another option is to have a server side script which would stream the pdf and set this header.


You'll need to dynamicaly force attachement headers using a server side script like PHP. Here's an example using PHP :

<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
?>

Edit :

$file = $_GET['file']; 
readfile($file."pdf");  // Before doing this, check whather the user have permission to download that file.

call your script : download.php?file=document will download document.pdf

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜