当“在浏览器中显示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."\";");
        }

其他回答

从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."\";");
        }

正确的格式是application/pdf,而不是application/force-download。这看起来像是对一些传统浏览器的黑客攻击。如果可以,总是使用正确的mimetype。

如果你可以控制服务器代码:

强制下载/提示:使用头(“Content-Disposition”,“附件;文件名= myfilename.myextension "); 浏览器尝试打开它:使用头("Content-Disposition", "inline;文件名= myfilename.myextension ");

无法控制服务器代码:

使用HTML5下载属性。它使用在视图端指定的自定义文件名。

注意:我更喜欢在服务器端设置文件名,因为您可能有更多的信息,可以使用公共代码。

下面是在PHP中强制在浏览器中查看文件的另一种方法:

$extension = pathinfo($file_name, PATHINFO_EXTENSION);
$url = 'uploads/'.$file_name;
        echo '<html>'
                .header('Content-Type: application/'.$extension).'<br>'
                .header('Content-Disposition: inline; filename="'.$file_name.'"').'<br>'
                .'<body>'
                .'<object   style="overflow: hidden; height: 100%;
             width: 100%; position: absolute;" height="100%" width="100%" data="'.$url.'" type="application/'.$extension.'">
                    <embed src="'.$url.'" type="application/'.$extension.'" />
             </object>'
            .'</body>'
            . '</html>';

为了向浏览器表明文件应该在浏览器中查看,HTTP响应应该包括这些头文件:

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

下载而不是查看文件:

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

如果文件名包含特殊字符(如文件名[1].pdf),则文件名周围的引号是必需的,否则可能破坏浏览器处理响应的能力。

如何设置HTTP响应标头取决于您的HTTP服务器(或者,如果您从服务器端代码生成PDF响应,则需要使用服务器端编程语言)。

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

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