gpt4 book ai didi

c# - 使用 标签强制下载 pdf

转载 作者:行者123 更新时间:2023-11-30 18:32:24 27 4
gpt4 key购买 nike

我正在尝试使用标签强制下载 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();
}

看起来页面只是刷新,没有文件正在下载。

编辑:

所以我已经尝试将它切换为 LinkBut​​ton,但它仍然无法产生类似的效果。

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

最佳答案

试试这个。

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>

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com