如何制作PDF文件的可下载在HTML link?
题
我给链接的pdf文件在我的网页下载,如下面
<a href="myfile.pdf">Download Brochure</a>
问题是当用户击这个链接
- 如果用户必须安装Adobe Acrobat,然后它打开了文件中的同一浏览器窗口在Adobe读者。
- 如果Adobe Acrobat是没有安装那就弹出来的用户下载的文件。
但我希望它总是弹出来的用户下载,不论"Adobe acrobat"是否安装)。
请告诉我,我怎么可以这样做?
解决方案
而不是链接。PDF文件,而不是做这样的事情
<a href="pdf_server.php?file=pdffilename">Download my eBook</a>
它输出一个自定义头,打开了PDF(binary safe)和打印数据的用户浏览器,然后,他们可以选择保存PDF尽管他们的浏览器的设置。的pdf_server.php 应该是这样的:
header("Content-Type: application/octet-stream");
$file = $_GET["file"] .".pdf";
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp);
PS:显然运行的一些理智的检查,在"文件"变量,以防止有人偷了你的文件例如不接受的文件的扩展,否认斜杠,添加。pdf值
其他提示
这是一个共同的问题,但很少有人知道有一个简单HTML5解决方案:
<a href="./directory/yourfile.pdf" download="newfilename">Download the pdf</a>
哪里 newfilename
是所建议的文件名为用户节省的文件。或者它将默认文件上的服务器端,如果你离开它空了,像这样:
<a href="./directory/yourfile.pdf" download>Download the pdf</a>
兼容性:我测试了这个火狐21和铁,这两个运行良好。它可能不HTML5-不兼容或过时的浏览器。只有我浏览器进行测试,没有强迫载即...
不要循环 通过每个文件线。使用 readfile 相反,其速度更快。这是关闭的php站:http://php.net/manual/en/function.readfile.php
$file = $_GET["file"];
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' . urlencode(basename($file)));
// header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
确保清理你的获得变作为人可以下载一些php文件...
而不是使用a PHP script,阅读和刷新文件,它是更简洁改写了使用头 .htaccess
.这将保留一个"好"网址(myfile.pdf
而不是的 download.php?myfile
).
<FilesMatch "\.pdf$">
ForceType applicaton/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
我找到了一种方法来这样做,与普通的老HTML和JavaScript级,降低优雅。测试在IE7-10、野生动物园,铬和FF:
HTML下链接:
<p>Thanks for downloading! If your download doesn't start shortly,
<a id="downloadLink" href="...yourpdf.pdf" target="_blank"
type="application/octet-stream" download="yourpdf.pdf">click here</a>.</p>
jQuery(纯JavaScript code会更详细的),模拟点击的链接,后一个小时延:
var delay = 3000;
window.setTimeout(function(){$('#downloadLink')[0].click();},delay);
使这个更强大的你可以添加HTML5功能检测如果不是,那么使用 window.open()
打开一个新的窗口,有的文件。
这是关键:
header("Content-Type: application/octet-stream");
内容类型application/x-pdf-document或application/pdf发送而发送PDF格式的文件。Adobe的阅读器,通常设置的处理程序对于这种类型所以浏览器将通过的文件Adobe读者当中的任何PDF MIME类型是收到。
有一个更简单的方式在HTML5:增加一个 Download
属性。
它支持多数现代浏览器。
<a download href="file.pdf">Download PDF</a>
我知道我很晚要回答这个问题,但我发现一个黑客要做到这一点,在javascript。
function downloadFile(src){
var link=document.createElement('a');
document.body.appendChild(link);
link.href= src;
link.download = '';
link.click();
}
试试这个:
<a href="pdf_server_with_path.php?file=pdffilename&path=http://myurl.com/mypath/">Download my eBook</a>
在代码里pdf_server_with_path.php 为:
header("Content-Type: application/octet-stream");
$file = $_GET["file"] .".pdf";
$path = $_GET["path"];
$fullfile = $path.$file;
header("Content-Disposition: attachment; filename=" . Urlencode($file));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . Filesize($fullfile));
flush(); // this doesn't really matter.
$fp = fopen($fullfile, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp);
这里有一个不同的方法。我更喜欢而不是依靠浏览器支撑,或者地址这一在应用层,来使用网络服务器的逻辑。
如果您使用Apache,可以把一个.就文件中相关目录可以使用代码如下。当然,你可以把这个放在httpd.conf好吧,如果你访问。
<FilesMatch "\.(?i:pdf)$">
Header set Content-Disposition attachment
</FilesMatch>
该FilesMatch指令仅仅是一个regex,因此它可能被设置为粒度,只要你想,或者你可以加入其他的扩展。
头行做同样的事情的第一线,在PHP scripts以上。如果你需要设定的内容类型的行为好,你可以这样做的同样方式,但是我没找到必要的。
我解决了地雷使用的整个网址的PDF文件(而不是仅仅把该文件的名称或位置来href):a href="领域。com/pdf/filename.pdf"
如果你需要限制下载率,使用这个代码!
<?php
$local_file = 'file.zip';
$download_file = 'name.zip';
// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5;
if(file_exists($local_file) && is_file($local_file))
{
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($local_file));
header('Content-Disposition: filename='.$download_file);
flush();
$file = fopen($local_file, "r");
while(!feof($file))
{
// send the current file part to the browser
print fread($file, round($download_rate * 1024));
// flush the content to the browser
flush();
// sleep one second
sleep(1);
}
fclose($file);}
else {
die('Error: The file '.$local_file.' does not exist!');
}
?>
更多信息 按这里
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File Uploader</title>
<script src="../Script/angular1.3.8.js"></script>
<script src="../Script/angular-route.js"></script>
<script src="../UserScript/MyApp.js"></script>
<script src="../UserScript/FileUploder.js"></script>
<>
.percent {
position: absolute;
width: 300px;
height: 14px;
z-index: 1;
text-align: center;
font-size: 0.8em;
color: white;
}
.progress-bar {
width: 300px;
height: 14px;
border-radius: 10px;
border: 1px solid #CCC;
background-image: -webkit-gradient(linear, left top, left bottom, from(#6666cc), to(#4b4b95));
border-image: initial;
}
.uploaded {
padding: 0;
height: 14px;
border-radius: 10px;
background-image: -webkit-gradient(linear, left top, left bottom, from(#66cc00), to(#4b9500));
border-image: initial;
}
</>
</head>
<body ng-app="MyApp" ng-controller="FileUploder">
<div>
<table ="width:100%;border:solid;">
<tr>
<td>Select File</td>
<td>
<input type="file" ng-model-instant id="fileToUpload" onchange="angular.element(this).scope().setFiles(this)" />
</td>
</tr>
<tr>
<td>File Size</td>
<td>
<div ng-repeat="file in files.slice(0)">
<span ng-switch="file.size > 1024*1024">
<span ng-switch-when="true">{{file.size / 1024 / 1024 | number:2}} MB</span>
<span ng-switch-default>{{file.size / 1024 | number:2}} kB</span>
</span>
</div>
</td>
</tr>
<tr>
<td>
File Attach Status
</td>
<td>{{AttachStatus}}</td>
</tr>
<tr>
<td>
<input type="button" value="Upload" ng-click="fnUpload();" />
</td>
<td>
<input type="button" value="DownLoad" ng-click="fnDownLoad();" />
</td>
</tr>
</table>
</div>
</body>
</html>
在红宝石在轨道上应用程序(特别是有点像虾宝石和Prawnto轨插件),可以完成这一多一点,只需比一个完整的脚本(如同以前的PHP例)。
在你的控制器:
def index
respond_to do |format|
format.html # Your HTML view
format.pdf { render :layout => false }
end
end
渲染:布局=>false部告诉浏览器来开放"你想下载这个文件?"的提示,而不是试图渲染PDF。然后你就可以链接到文件通常: http://mysite.com/myawesomepdf.pdf