【问题标题】:How to make a PDF downloadable so it doesn't open up in new window如何使 PDF 可下载,使其不在新窗口中打开
【发布时间】:2012-09-01 10:51:54
【问题描述】:

我在 Stack 上找到了一个链接,该链接解释了如何执行此操作,但它仅用 PHP 术语解释。我想在我的网站上有指向 PDF 文件的链接,以便它自动转到“另存为”屏幕,而不是在网页表单中打开。这样,最终用户将能够更轻松地下载它,因为我的很多客户不知道如何在网页上执行文件/另存为。

所以我想要一个链接,例如:

<a href="brochure.pdf" target="_blank">Download Brochure</a>

然后它会直接进入“另存为”框,并允许客户将 PDF 手册保存到他/她的计算机上,而无需打开另一个网页。任何帮助将不胜感激!

【问题讨论】:

  • 如果你删除了目标,并且在标题中包含attachment;,那么你得到了你想要的。
  • 感谢 Aristos .... 我摆脱了目标,这很有帮助。感谢您抽出宝贵时间回复。

标签: asp.net pdf download


【解决方案1】:

如果您将“brochure.pdf”替换为在 Page_Load 事件中包含以下代码的 asp.net 页面,您将获得所需的行为:

  string filename = "brochure.pdf";
  Response.ContentType = "image/pdf";
  string headerValue = string.Format("attachment; filename={0}", filename);
  Response.AppendHeader("Content-Disposition", headerValue);
  Response.TransmitFile(Server.MapPath(filename));
  Response.End();

另一种方法是包含一个包含上述代码的链接按钮。为安全起见,请确保验证文件名,但您分配它,这样用户就无法访问任意文件。

【讨论】:

  • 嗨,汤姆,感谢您抽出宝贵时间回复。我在做这个工作。看来您的答案与伊卡洛斯给出的答案相似。作为一个非程序员,我创建了一个页面,DownloadMgr.aspx,但我不确定将您建议的代码放在哪里,因为我什至不知道 page_load 在哪里。再次感谢您的帮助!
  • 您的 aspx 页面有一个支持的 DownloadMgr.aspx.cs 文件。这将有一个 page_load 事件。上面的代码是 C#。只需将上面的代码放入其中即可。
  • 嘿汤姆...我的 DownloadMGR.aspx.vb 文件只是将这个作为后面的代码:Partial Class products_DownloadMgr Inherits System.Web.UI.Page End Class
  • 但是你和伊卡洛斯绝对让我走上了正确的道路,我会通过反复试验来解决这个问题。非常感谢您抽出时间来帮助我,我会完成这项工作。再次感谢!
【解决方案2】:

除非您将 Content-Disposition 标头添加到响应中,否则您不能仅从超链接(或可靠地使用 Javascript)强制下载文件;因此,解决此问题并始终强制下载文件的一种方法可能是拥有一个中间页面,该页面将为您添加标题。

因此,您的链接必须变成这样:

<a href="DownloadMgr.aspx?File=brochure.pdf" target="_blank">Download Brochure</a>

DownloadMgr.aspx 应该是这样的:

if(!IsPostback)
{
    if(Request.QueryString["File"]!=null) 
    {  
       if(Request.QueryString["File"].Contains("pdf")))
          Response.ContentType = "application/pdf"; //varies depending on the file being streamed
   Response.AddHeader("Content-Disposition", "attachment; filename=" +  Request.QueryString["File"]);
   Response.WriteFile(Server.MapPath(Request.QueryString["File"]));                
}

不过,更好的方法是创建一个 HTTPHandler 来做同样的事情。您可以查看this answer 以获取有关如何执行此操作的更多详细信息。创建 HTTPHandler 的好处之一是它不涉及常规 aspx 页面所需的所有处理、初始化等。

完整代码示例:

<%@ Language=C# %>
<HTML>
<head>
    <title>Download Manager</title>
</head>
   <script runat="server" language="C#">
       void Page_Load(object sender, EventArgs e)
       {
           if (!this.Page.IsPostBack)
           {
               if (Request.QueryString["File"] != null)
               {
                   if (Request.QueryString["File"].Contains("pdf"))
                       Response.ContentType = "application/pdf"; //varies depending on the file being streamed
                   Response.AddHeader("Content-Disposition", "attachment; filename=" + Request.QueryString["File"]);
                   Response.WriteFile(Server.MapPath(Request.QueryString["File"]));
               }
           }
       }
   </script>
   <body>
      <form id="form" runat="server">

      </form>
   </body>
</HTML>

VB.NET 版本

<%@ Language="VB" %>
<HTML>
<head>
    <title>Download Manager</title>
</head>
   <script runat="server" language="VB">
       Sub Page_Load()
           If (Not Page.IsPostBack) Then

               If (Request.QueryString("File") IsNot Nothing) Then
                   If (Request.QueryString("File").Contains("pdf")) Then
                       Response.ContentType = "application/pdf" 'varies depending on the file being streamed
                   End If
                   Response.AddHeader("Content-Disposition", "attachment; filename=" + Request.QueryString("File"))
                   Response.WriteFile(Server.MapPath(Request.QueryString("File")))
               End If
           End If
       End Sub
   </script>
   <body>
      <form id="form" runat="server">

      </form>
   </body>
</HTML>

现在将上述代码保存为DownloadMgr.aspx 并将其放入您的网站中。

【讨论】:

  • 嗨,伊卡洛斯。看起来你和汤姆有相似的想法,只是处理方式略有不同。我确实创建了一个 DownloadMgr.aspx 页面,但作为一个非程序员,我对将您提供的编码放在哪里有点困惑,因为我不知道 page_load 事件在哪里。非常感谢您花时间回复。我会继续努力的!
  • 非常感谢伊卡洛斯。它告诉我
  • ifs 在 VS 2010 中带有下划线,并表示它们不能出现在方法主体/多行 lambda 之外,并且响应以波浪形蓝色下划线并表示预期的声明。但是你肯定让我走上了正确的道路——我会通过反复试验来启发式地解决这个问题。非常感谢您花时间帮助我。我会得到这个工作!
  • @JasonWeber 抱歉,我发布的代码缺少 } 导致一些错误。我再次更新了代码。再次复制并粘贴。
  • 嘿@Icarus -- 我试图通过 Telerik 的代码转换器运行修改后的代码,因为我的网站在 vb 中,它给了我这个错误:转换错误:代码无法转换。详细信息: -- line 2 col 4: "}" expected 请检查原始代码中是否有任何错误,然后重试。
【解决方案3】:

如果有人需要知道,为了将来参考,我结合了 Tom 和 Icarus 的答案找到了答案。我只需要将一些 C# 转换为 VB。无论如何,我的超链接是:

<a href="DownloadMgr.aspx">Download Brochure</a>

我的DownloadMgr.aspx.vb后面的代码是这样的:

Partial Class products_DownloadMgr
    Inherits System.Web.UI.Page

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    Dim filename As String = "USS_Exact_Scan_Introduction.pdf"
    Response.ContentType = "image/pdf"
    Dim headerValue As String = String.Format("attachment; filename={0}", filename)
    Response.AppendHeader("Content-Disposition", headerValue)
    Response.TransmitFile(Server.MapPath(filename))
    Response.[End]()

End Sub

End Class

非常感谢 Tom 和 Iracus 抽出时间提供帮助!

【讨论】:

    猜你喜欢
    • 2020-04-16
    • 2012-09-01
    • 2022-06-19
    • 2014-07-01
    • 1970-01-01
    • 2022-12-09
    • 2012-01-07
    • 2015-06-03
    • 2013-06-17
    相关资源
    最近更新 更多