如何强制PDF下载初学者 [英] How to force PDF to download beginner

查看:171
本文介绍了如何强制PDF下载初学者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

如何自动强制下载pdf?






<我已经检查了其他论坛,但我不明白他们说了什么。我已经检查过这个链接,人们似乎可以得到解决方案:

http://css-tricks.com/snippets/htaccess/force-files-to-download-not-open-in -browser /



我想要做一个下载PDF的按钮,该文件太大而无法在线查看。所以我想要那个按钮来强制浏览器下载文件。询问或不询问用户。将有不同的PDF名称文件,所以我需要使用各种PDF文件的PDF格式下载此按钮。



我查了不同的网页,看起来它可以是完成使用htaccess,但我真的不知道如何做到这一点。有人可以帮助告诉我怎样才能做到这一点!?!?!有没有一种简单的方法通过CSS / html?



我已经坚持了很长一段时间这样一个小细节。



我的代码很简单:

 < div class =图标容器> 
< a href =hugefile.pdfalt =>< img href =icon.pngalt =/>
< / div>

有人可以给我这个吗?

解决方案

您需要修改返回给客户端的HTTP头以完成此操作:



以下是PHP代码示例:

  $ path =path / to / file.pdf; 
$ filename =file.pdf;
header('Content-Transfer-Encoding:binary'); //对于Gecko浏览器,主要是
header('Last-Modified:'。gmdate('D,d M Y H:i:s',filemtime($ path))。'GMT');
头('Accept-Ranges:bytes'); //下载简历
header('Content-Length:'。filesize($ path)); //文件大小
header('Content-Encoding:none');
header('Content-Type:application / pdf'); //如果文件不是PDF
header('Content-Disposition:attachment; filename ='。$ filename),则更改此mime类型; //使浏览器显示另存为对话框
readfile($ path); //这是实际下载文件所必需的,否则它将是0Kb


Possible Duplicate:
How to force a pdf download automatically?

I've checked other forums, but I do not understand what they say. I've checked this link which people do seem to get the solution:

http://css-tricks.com/snippets/htaccess/force-files-to-download-not-open-in-browser/

I want to do a button which is done for downloading a PDF, the file is too big to see it online. So I want that button to force the browser to download the file. Asking or not asking the user. There will be different PDF name files so I need to make this button downloadable with PDF formats of various PDF files.

I've checked different webs and it seems it can be done using htaccess, but I honestly have no clue of how to do this. Can somebody help to show me how I can do this!?!?! Is there a simple way via CSS/html??

I've been stuck for a long while for such a small detail.

My code is simply:

<div class="Container for icons">
<a href="hugefile.pdf" alt=""><img href="icon.png" alt=""/>
</div>

Could somebody give me hand on this one?

解决方案

You'll need to modify the HTTP header returned to the client in order to accomplish this:

Here's a PHP code example:

$path = "path/to/file.pdf";
$filename = "file.pdf";
header('Content-Transfer-Encoding: binary');  // For Gecko browsers mainly
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
header('Accept-Ranges: bytes');  // For download resume
header('Content-Length: ' . filesize($path));  // File size
header('Content-Encoding: none');
header('Content-Type: application/pdf');  // Change this mime type if the file is not PDF
header('Content-Disposition: attachment; filename=' . $filename);  // Make the browser display the Save As dialog
readfile($path);  //this is necessary in order to get it to actually download the file, otherwise it will be 0Kb

这篇关于如何强制PDF下载初学者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆