如何通过HTML链接下载PDF文件?

问题描述 投票:119回答:13

我正在网页上提供pdf文件的链接以供下载,如下所示>>

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

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

  • 如果用户已经安装了Adobe Acrobat,则它将在Adobe Reader的同一浏览器窗口中打开文件。
  • 如果未安装Adobe Acrobat,则弹出窗口以供用户下载文件。
  • 但是无论是否安装了“ Adob​​e acrobat”,我都希望它始终弹出用户下载。

    请告诉我我该怎么做?

我在我的网页上提供了pdf文件的链接供下载,如下Download Brochure所示,问题是当用户单击此链接时,如果用户有...

php pdf xhtml download markup
13个回答
116
投票

而不是链接到.PDF文件,而是执行类似的操作

<a href="pdf_server.php?file=pdffilename">Download my eBook</a>

0
投票

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


0
投票

如果您需要限制下载速度,请使用此代码!

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

?>

0
投票
<!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>   

-1
投票

[在Ruby on Rails应用程序中(尤其是使用Prawn gem和Prawnto Rails插件之类的东西,您可以比使用完整脚本(如先前的PHP示例)更简单地完成此操作。

在您的控制器中:


-1
投票

[在Ruby on Rails应用程序中(尤其是使用Prawn gem和Prawnto Rails插件之类的东西,您可以比使用完整脚本(如先前的PHP示例)更简单地完成此操作。

在您的控制器中:


192
投票

这是一个常见问题,但很少有人知道有一个简单的HTML 5解决方案:

<a href="./directory/yourfile.pdf" download="newfilename">Download the pdf</a>

49
投票

Do n't loop

在每个文件行中。使用readfile

24
投票

不是使用PHP脚本来读取和刷新文件,而是使用.htaccess重写标头更为简洁。这将保留一个“不错的” URL(myfile.pdf而不是download.php?myfile)。

<FilesMatch "\.pdf$">
ForceType applicaton/octet-stream
Header set Content-Disposition attachment
</FilesMatch>

14
投票

我找到了一种方法,可以对普通的旧HTML和JavaScript / jQuery进行适当的降级。在IE7-10,Safari,Chrome和FF中测试:

HTML下载链接:


5
投票

这是关键:

header("Content-Type: application/octet-stream");

3
投票

我知道我回答这个问题很晚了,但是我发现在javascript中可以做到这一点。

function downloadFile(src){
    var link=document.createElement('a');
    document.body.appendChild(link);
    link.href= src;
    link.download = '';
    link.click();
}

0
投票

尝试一下:

<a href="pdf_server_with_path.php?file=pdffilename&path=http://myurl.com/mypath/">Download my eBook</a>


0
投票

这是另一种方法。我宁愿不依靠浏览器支持或在应用程序层解决这个问题,而是使用Web服务器逻辑。

如果使用Apache,并且可以将.htaccess文件放在相关目录中,则可以使用以下代码。当然,如果可以访问,也可以将其放在httpd.conf中。

© www.soinside.com 2019 - 2024. All rights reserved.