使用 C# 将下载添加到定位标记

本文关键字:定位 添加 下载 使用 | 更新日期: 2023-09-27 18:37:19

所以基本上html看起来像这样

<a href='test.pdf' Download>download test</a>

但是我需要用 C# 制作这个,到目前为止我所拥有的是

HtmlAnchor link = new HtmlAnchor();
link.Href = "test.pdf";
link.innerText = "download test";

我如何放入"下载"部分,以便当您单击链接时,它实际上会下载文件而不是链接到它?

使用 C# 将下载添加到定位标记

您需要使用InnerHtml而不是InnerText以及粗体的<b>

link.InnerHtml = @"<b>download test</b>";

基于 OP 编辑进行编辑,

您将需要在linkButton单击事件上使用Response.WriteFile,您可能会在这篇文章中寻找被问到的内容。

FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ContentType = "Application/msword";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
Response.WriteFile(fileInfo.FullName);
Response.End();

试试这个:放在你的html页面中,在C#中,写:litdoc。文本 += " + "下载测试" + "; 在处理程序中:提及下载pdf文件的代码,就像:

    string file = "";
    file = context.Request.QueryString["file"]; 
    if (file != "")
    {
        context.Response.Clear(); 
        context.Response.ContentType = "application/octet-stream";
        context.Response.AddHeader("content-disposition", "attachment;filename="    +           Path.GetFileName(file));
        context.Response.WriteFile(file);
        context.Response.End();
    }

其中 path 是您下载 PDF 文件的位置。