【发布时间】:2011-09-11 17:47:42
【问题描述】:
当“在浏览器中显示 PDF”选项未选中时,有没有办法强制 PDF 文件在浏览器中打开?
我尝试使用 embed 标记和 iframe,但它仅在选中该选项时才有效。
我能做什么?
【问题讨论】:
标签: pdf http-headers browser embed embedding
当“在浏览器中显示 PDF”选项未选中时,有没有办法强制 PDF 文件在浏览器中打开?
我尝试使用 embed 标记和 iframe,但它仅在选中该选项时才有效。
我能做什么?
【问题讨论】:
标签: pdf http-headers browser embed embedding
如果您链接到 .PDF,它将在浏览器中打开。
如果未选中该框,则应链接到 .zip 以强制下载。
如果 .zip 不是一个选项,则在 PHP 中使用 headers 强制下载
header('Content-Type: application/force-download');
header('Content-Description: File Transfer');
【讨论】:
【讨论】:
为了向浏览器指示应该在浏览器中查看文件,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>
如果 PDF 文件位于某个文件夹中,并且该文件夹无权直接访问该文件夹中的文件,那么您必须通过这种方式使用 .htaccess 文件设置绕过一些文件访问限制:
<FilesMatch ".*\.(jpe?g|JPE?G|gif|GIF|png|PNG|swf|SWF|pdf|PDF)$" >
Order Allow,Deny
Allow from all
</FilesMatch>
但现在只允许某些必要的文件。
我用过这段代码,效果很好。
【讨论】:
如果您使用的是 HTML5(我猜现在每个人都在使用它),有一个名为 download 的属性。
例如,
<a href="somepathto.pdf" download="filename">
这里filename 是可选的,但如果提供,它将使用此名称作为下载文件。
【讨论】:
从根文件打开 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。对于某些旧版浏览器来说,这看起来像是一个 hack。如果可以,请始终使用正确的 mimetype。
如果您可以控制服务器代码:
header("Content-Disposition", "attachment; filename=myfilename.myextension");
header("Content-Disposition", "inline; filename=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>';
【讨论】:
header 在您开始输出请求正文后不起作用(并且它不会返回用于在 HTML 文档中连接的字符串)
只需打开 Adobe Reader,菜单 → 编辑 → 首选项 → Internet,然后切换到浏览器模式或在不同浏览器上的详细说明尝试Display PDF in browser | Acrobat, Acrobat Reader.
【讨论】:
如果您有 Apache,请将其添加到 .htaccess 文件中:
<FilesMatch "\.(?i:pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
【讨论】:
这适用于 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>
在您的控制器中:
public ActionResult Download(Guid id)
{
if (id == Guid.Empty)
return null;
var model = GetModel(id);
return File(model.FilePath, "application/pdf", model.FileName);
}
public FileStreamResult View(Guid id)
{
if (id == Guid.Empty)
return null;
var model = GetModel(id);
FileStream fs = new FileStream(model.FilePath, FileMode.Open, FileAccess.Read);
return File(fs, "application/pdf");
}
【讨论】:
虽然以下内容在 Firefox 上运行良好,但不适用于 chrome 和移动浏览器。
Content-Type: application/pdf
Content-Disposition: inline; filename="filename.pdf"
要修复 chrome 和移动浏览器错误,请执行以下操作:
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 设置更改为您需要的任何内容。
【讨论】: