为什么下载的文件没有下载,而是在浏览器中显示? [英] Why downloaded file is not downloaded instead it is shown in browser?
问题描述
我将网站中的访问者从页面A重定向到页面B.在第B页中,我希望用户获取下载的PDF文件(在加载B页时下载)。
我已经从另一篇文章中获取了代码(参见上一个问题在这里回答),而我的代码页B是如下:
I redirect the visitors in my website from page A to page B. In page B I expect users to get the downloaded PDF file (to be downloaded when page B is loading). I have taken the code from another article (see a previous question answered here) and my code of page B is the following:
<?php
header('Content-Disposition: attachment; filename=nature.pdf');
header('Content-type: application/pdf');
$fn=fopen("/wp-content/nature.pdf","r");
fpassthru($fn);
?>
输出不是通过打开下载对话框,而是在浏览器中显示一些不可读的字符,例如以下(我刚刚收到一个小样本):
The output is not by opening a download dialog box, instead some unreadable characters are displayed in browser such as the following (I have just picked up a small sample below):
%PDF-1.4% 3 0 obj< > streamx MK1 o + $ zIg& V =T =Xo K i+#V yx3 (BX pW`
%PDF-1.4 %���� 3 0 obj <>stream x���MK1�o�+�$zIg&�� V=T�=Xo����K��i+#V�yx3��(BX�pW`
服务器:OS Linux; PHP版本:5.2.17
Server: OS Linux; PHP version: 5.2.17
访问者 - >浏览器:Firefox;操作系统:Windows 2000
The visitor -> Browser: Firefox; OS: Windows 2000
是否可能由于客户端的旧操作系统而失败?如果没有,有没有人知道如何强制下载的解决方案?非常感谢。
Is it possible to fail due to the old OS on client side? If not, does anybody know a solution how to force the download? Any help would be highly appreciated.
谢谢。
推荐答案
内容长度标题:
ob_clean(); ob_start();
header('Content-Disposition: attachment; filename=nature.pdf');
header('Content-type: application/pdf');
header ("Content-Length: ".filesize("/wp-content/nature.pdf"));
readfile("/wp-content/nature.pdf");
exit;
这篇关于为什么下载的文件没有下载,而是在浏览器中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!