如何在HTML链接中实现PDF文件下载?

149

我在网页上提供了一个PDF文件的下载链接,如下所示:

<a href="myfile.pdf">Download Brochure</a>

问题是当用户单击此链接时,

  • 如果用户已安装Adobe Acrobat,则在Adobe Reader中的同一浏览器窗口中打开文件。
  • 如果未安装Adobe Acrobat,则弹出下载文件的提示框。

但我希望它始终弹出下载提示框,无论"Adobe Acrobat"是否安装。

请告诉我如何实现这个功能?

14个回答

-1
我解决了我的问题,使用整个PDF文件的网址(而不仅仅是将文件名或位置放入href中): a href="domain.com/pdf/filename.pdf"

-1
在Ruby on Rails应用程序中(特别是使用Prawn gem和Prawnto Rails插件),您可以比完整的脚本(如前面的PHP示例)更简单地实现此目标。
在您的控制器中:
def index

 respond_to do |format|
   format.html # Your HTML view
   format.pdf { render :layout => false }
 end
end

render :layout => false 这部分告诉浏览器打开“您是否要下载此文件?”提示,而不是尝试呈现 PDF。然后,您就可以正常链接到该文件:http://mysite.com/myawesomepdf.pdf


在哪里编写这段代码?在哪个控制器中?我是 PHP 新手,请解释一下。 - djmzfKnm
@Prashant 这不是 PHP,而是 Ruby on Rails。 - eloyesp
@Prashant:如果你需要一个PHP示例,请查看线程中更早的示例,但请阅读有关它们可能存在不安全性的评论。 - Evan Donovan

-1

如果你需要限制下载速率,可以使用这段代码!!

<?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!');
}

?>

欲了解更多信息请点击此处


-1
<!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>   

3
您应该解释您在代码中提供的内容。即使只是简略地介绍也可以,如果不需要详细介绍,请勿强求。 - Suraj Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接