You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chromium浏览器中target="_blank"无法打开PDF新标签页问题求助

解决Chromium中PDF链接无法通过target="_blank"在新标签页打开的问题

问题原因

出现这个问题通常有两个核心原因:

  • 服务器返回PDF文件时,Content-Disposition响应头被设置为attachment,强制浏览器触发下载而非在线预览
  • Chromium内置的PDF预览插件被禁用,或浏览器默认行为被修改

解决方案

1. 后端修正(推荐,从根源解决)

在cshtml对应的ASP.NET后端代码中,确保返回PDF时设置正确的响应头,指定文件为inline(在线预览模式):

ASP.NET MVC 示例

public ActionResult DownloadTermsPdf()
{
    var pdfFilePath = Server.MapPath("~/Files/TermsOfUse.pdf");
    byte[] pdfBytes = System.IO.File.ReadAllBytes(pdfFilePath);

    // 设置响应头,允许浏览器在线预览PDF
    Response.Headers.Add("Content-Disposition", "inline; filename=TermsOfUse.pdf");
    return File(pdfBytes, "application/pdf");
}

对应的cshtml链接修改为指向该Action:

<a href="@Url.Action("DownloadTermsPdf", "Home")" class="some-class" target="_blank">Terms of Use</a>

2. 前端临时兼容方案

如果无法修改后端代码,可通过JavaScript手动创建新标签页打开PDF,绕过响应头限制:

<a href="path/to/terms.pdf" class="some-class" onclick="handlePdfOpen(event, this.href)">Terms of Use</a>

<script>
function handlePdfOpen(event, pdfUrl) {
    event.preventDefault();
    const newTab = window.open('', '_blank');
    newTab.location.href = pdfUrl;
}
</script>

3. 检查Chromium浏览器设置

确认浏览器的PDF预览功能未被禁用:

  • 在地址栏输入 chrome://settings/content/pdfDocuments
  • 确保「下载PDF文件而不是在Chrome中自动打开它们」选项处于未勾选状态

内容的提问来源于stack exchange,提问作者Md Ahteshamul Hussain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 11:40:58