强制下载带有 标签的 pdf

本文关键字:标签 pdf 下载 | 更新日期: 2023-09-27 18:30:52

我正在尝试强制下载带有标签的pdf。

这是我所拥有的:

 <a ID="personal_training_form" class="pdfcalltoaction" OnServerClick="personal_training_form_Click" runat="server">PERSONAL TRAINING FORM</a>

和我的代码隐藏:

public void personal_training_form_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.ContentType = "application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=PersonalTrainingForm.pdf");
    Response.TransmitFile("~/programs/PERSONAL_TRAINING_FORM_to_mail.pdf");
    Response.End();
} 

页面似乎只是在刷新,没有下载任何文件。

编辑:

所以我尝试将其切换到 LinkButton,但它仍然无法处理类似的效果。

<asp:LinkButton ID="personal_training_form" CssClass="pdfcalltoaction" runat="server" OnClick="personal_training_form_Click">Personal Training Form</asp:LinkButton>

强制下载带有 <a> 标签的 pdf

试试这个。

public void download_Click(Object sender, EventArgs e)
{
    Response.AddHeader("Content-Type", "application/octet-stream");
    Response.AddHeader("Content-Transfer-Encoding","Binary");
    Response.AddHeader("Content-disposition", "attachment; filename='"sample.pdf'"");
    Response.WriteFile(HttpRuntime.AppDomainAppPath + @"path'to'file'sample.pdf");
    Response.End();
}
<asp:LinkButton ID="download" runat="server" OnClick="download_Click">Download</asp:LinkButton>