如何强制文件在浏览器中打开而不是下载(PDF)?

如何强制文件在浏览器中打开而不是下载(PDF)?,pdf,http-headers,browser,embed,embedding,Pdf,Http Headers,Browser,Embed,Embedding,如果未选中“在浏览器中显示PDF”选项,是否有办法强制在浏览器中打开PDF文件 我尝试使用嵌入标记和iframe,但它只有在选中该选项时才起作用 我能做什么?如果您链接到.PDF,它将在浏览器中打开。 如果未选中该框,则应链接到.zip以强制下载 如果.zip不是一个选项,那么使用PHP强制下载 header('Content-Type: application/force-download'); header('Content-Description: File Transfer');

如果未选中“在浏览器中显示PDF”选项,是否有办法强制在浏览器中打开PDF文件

我尝试使用嵌入标记和iframe,但它只有在选中该选项时才起作用


我能做什么?

如果您链接到.PDF,它将在浏览器中打开。
如果未选中该框,则应链接到.zip以强制下载

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

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


如果嵌入是一个选项或我的新插件,PIFF:

要向浏览器指示应在浏览器中查看文件,HTTP响应应包括以下标题:

Content-Type: application/pdf
Content-Disposition: inline; filename="filename.pdf"
要下载而不是查看文件,请执行以下操作:

Content-Type: application/pdf
Content-Disposition: attachment; filename="filename.pdf"
如果文件名包含特殊字符,如
filename[1].pdf,则需要在文件名周围加引号,否则可能会破坏浏览器处理响应的能力


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

您可以通过以下方式执行此操作:

<a href="path to PDF file">Open PDF</a>
但现在只允许某些必要的文件

我使用了这段代码,它工作得非常好。

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

比如说,

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


此处
filename
是可选的,但如果提供,则下载的文件将使用此名称。

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

哎呀,我上一篇文章中有输入错误

    header("Content-Type: application/force-download");
    header("Content-type: application/pdf");
    header("Content-Disposition: inline; filename=\"".$name."\";");

如果您不希望浏览器提示用户,则对第三个字符串使用“inline”而不是“attachment”。内联工作得很好。PDF将立即显示,而不要求用户单击“打开”。我使用了“附件”,这将提示用户打开、保存。我已尝试更改浏览器设置螺母,但它不会阻止提示。

对于pdf,正确的类型是
application/pdf
,而不是
application/force download
。这看起来像是对一些传统浏览器的攻击。如果可以,请始终使用正确的mimetype


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

  • 强制下载/提示:使用
    标题(“内容处置”、“附件;文件名=myfilename.myextension”)
  • 浏览器尝试打开它:使用
    标题(“内容处置”,“内联;文件名=myfilename.myextension”)
无法控制服务器代码:

  • 使用。它使用在视图侧指定的自定义文件名

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

以下是在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>';
$extension=pathinfo($file\u name,pathinfo\u extension);
$url='uploads/'。$file\u name;
回声“
.header('Content-Type:application/'.$extension)。'
' .header('Content-Disposition:inline;filename=“.”.$file_name.'”)。
' .'' .' ' .'' . '';
只需打开Adobe Reader的菜单→ 编辑→ 偏好→ Internet,然后切换到浏览器模式,或尝试获取不同浏览器上的详细说明。

如果您有Apache,请将其添加到
.htaccess
文件:

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

ForceType应用程序/八位组流
标题集内容处置附件

这是针对ASP.NET MVC的

在cshtml页面中:

<section>
    <h4><a href="@Url.Action("Download", "Document", new { id = @Model.GUID })"><i class="fa fa-download"></i> @Model.Name</a></h4>
    <object data="@Url.Action("View", "Document", new { id = @Model.GUID })" type="application/pdf" width="100%" height="800" class="col-md-12">
        <h2>Your browser does not support viewing PDFs, click on the link above to download the document.</h2>
    </object>
</section>

虽然以下内容在firefox上运行良好,但在chrome和移动浏览器上不起作用

Content-Type: application/pdf
Content-Disposition: inline; filename="filename.pdf"
要修复chrome和mobile浏览器错误,请执行以下操作:

        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浏览器
Google PDF Viewer可以这样使用:

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

我遇到了同样的问题,上面的大部分答案应该能解决你的问题。不幸的是,即使我收到了响应中的内容类型和内容处置标题,但我的pdf仍然是下载而不是查看的。经过数小时的头脑风暴和尝试

罪魁祸首是firefox,从某种程度上说,是我。紧张的笑声

默认情况下,当您在firefox中打开pdf文件时,它将为您提供一个弹出窗口,保存pdf文件直接打开它,并且还有一个复选框,上面写着从现在起自动执行此操作
并猜测是谁选择了它

由于这一错误,我的pdf被下载而不是查看,即使响应中有所有必需的标题。这是一个简单的错误,但花费了我大量的时间


要解决此问题,只需转到“设置”和“搜索应用程序”,然后将pdf设置更改为您需要的任何设置。

是的,但我需要一种方法强制在浏览器中打开而不下载。我不知道这是否可能。如果你不强迫它下载,那么你就是强迫它在浏览器中打开。如果无法在浏览器中打开,那是因为用户有特定的设置,您无法覆盖该设置,或者他们没有PDF阅读软件。这是错误的。没有应用程序/强制下载。您也可以使用alskjdsdjk/aljksdlasdj。浏览器将下载,因为它不知道此mime类型。正确的mime下载类型是application/octet-stream要强制下载,我宁愿使用内容类型:application/octet-stream.@PapickG.Taboada,但是用户的系统可能不知道文件类型。例如,某些用户可能选择“始终打开此文件”
<iframe src="http://docs.google.com/gview?url=http://example.com/path/to/my/directory/pdffile.pdf&embedded=true" frameborder="0"></iframe>