当“在浏览器中显示PDF”选项未选中时,是否有一种方法强制PDF文件在浏览器中打开?

我尝试使用嵌入标签和一个iframe,但它只有当该选项被选中时才会工作。

我该怎么办?


当前回答

从rootfile打开downloads.php。

然后在第186行修改如下:

        if(preg_match("/\.jpg|\.gif|\.png|\.jpeg/i", $name)){
            $mime = getimagesize($download_location);
            if(!empty($mime)) {
                header("Content-Type: {$mime['mime']}");
            }
        }
        elseif(preg_match("/\.pdf/i", $name)){
            header("Content-Type: application/force-download");
            header("Content-type: application/pdf");
            header("Content-Disposition: inline; filename=\"".$name."\";");
        }

        else{
            header("Content-Type: application/force-download");
            header("Content-type: application/octet-stream");
            header("Content-Disposition: attachment; filename=\"".$name."\";");
        }

其他回答

如果你链接到一个。pdf文件,它会在浏览器中打开。 如果该框未选中,它应该链接到一个。zip以强制下载。

如果.zip文件不是一个选项,那么使用PHP中的头文件强制下载

header('Content-Type: application/force-download'); 
header('Content-Description: File Transfer'); 

(我误解了问题,下面的答案是关于强制下载文件而不是在浏览器中打开文件)

如果你使用HTML5(我猜现在每个人都在用HTML5),有一个属性叫做下载。

例如,

<a href="somepathto.pdf" download="filename">

这里filename是可选的,但是如果提供了,它将使用这个名称下载文件。

EDIT

我知道这和问题的答案正好相反。我为那些(像我一样)寻找相反问题的人保留了相反的答案(证据:这个答案的赞票比反对票多)

如果你有Apache,把这个添加到。htaccess文件中:

<FilesMatch "\.(?i:pdf)$">
    ForceType application/octet-stream
    Header set Content-Disposition attachment
</FilesMatch>

虽然下面的工作很好在firefox,它不工作在chrome和移动浏览器。

Content-Type: application/pdf
Content-Disposition: inline; filename="filename.pdf"

要修复chrome和移动浏览器错误,请执行以下操作:

将文件存储在项目中的某个目录中 使用谷歌PDF查看器

谷歌PDF查看器可以这样使用:

<iframe src="http://docs.google.com/gview?url=http://example.com/path/to/my/directory/pdffile.pdf&embedded=true" frameborder="0"></iframe>

对于大文件,你需要让你的输出缓冲区开始添加

ob_start ();//在开始

. ./ /你的代码

Ob_clean();//在文件的末尾